Silverlight Load Client Image 加载客户端图片
Silverlight Load Client Image 加载客户端图片
这里做了一个用Silverlight加载客户端图片的例子。并且用了一个最简单的数据双向绑定。
beginning
这里例子的代码很简单不用做太多的说明。
前端界面设计
XAML:
<UserControl x:Class="LoadClientImage.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Loaded="UserControl_Loaded"> <Grid x:Name="LayoutRoot" Background="White" Margin="3" > <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Button HorizontalAlignment="Right" VerticalAlignment="Center" d:LayoutOverrides="Width"> <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal"> <Image Source="pixelicious_112.png" Stretch="None"/> <TextBlock VerticalAlignment="Center" Text="加载图片" TextWrapping="Wrap"/> </StackPanel> </Button> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="状态:" TextWrapping="Wrap" Margin="8,8,0,0"/> <TextBlock VerticalAlignment="Top" Text="{Binding Path=Status,Mode=TwoWay}" TextWrapping="Wrap" Margin="45,8,78,0"/> <Image Margin="3" x:Name="ImageHolder" Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="0" Stretch="UniformToFill" /> </Grid> </UserControl>
定义页面数据模型
pageModel:
public class pageModel : INotifyPropertyChanged {
private string _status = "等待加载";public string Status{get { return _status; }set{_status = value;PropertyChanged(this, new PropertyChangedEventArgs("Status"));}}
private BitmapImage _imageSource = new BitmapImage();public BitmapImage ImageSource {get { return _imageSource; }set{_imageSource = value;PropertyChanged(this, new PropertyChangedEventArgs("ImageSource"));}}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion}
后台逻辑代码
Page:
public partial class Page : UserControl{public pageModel model = new pageModel();public Page(){InitializeComponent();}
private void UserControl_Loaded(object sender, RoutedEventArgs e){this.DataContext = model;}
private void Button_Click(object sender, RoutedEventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Multiselect = false;bool? result = ofd.ShowDialog();if (!result.HasValue || result.Value == false)return;
BitmapImage imageSource = new BitmapImage();try{imageSource.SetSource(ofd.File.OpenRead());model.ImageSource = imageSource;model.Status = "加载 " + ofd.File.Name + " 成功";}catch (Exception){model.Status = "加载失败";}}
提示:不要忘记引入“BitmapImage”的命名空间:System.Windows.Media.Imaging; |
Source
http://cid-3e15d91acc4385a8.skydrive.live.com/embedrowdetail.aspx/Project/LoadClientImage