在 Silverlight 4 中使用 OpenFileDialog 浏览本地图片
代震军BLOG 上有一篇关于 在 Silverlight 中使用 OpenFileDialog 浏览本地图片 的文章,不过他是 Silverlight 2 下开发,而 Silverlight 的版本现在已经更新到 Silverlight 4 了,我将它下载下来研究,发现并不能直接在 Silverlight 4(Visual Studio 2010) 下运行。所以将其代码稍作了修改以便能在 Silverlight 4(Visual Studio 2010) 下运行,有需要的朋友可以在这里下载 源代码下载。
修改后的代码如下:
1. MainPage.xaml 文件
1 <UserControl x:Class="BrowsePhotos.MainPage"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 mc:Ignorable="d"
7 d:DesignHeight="300" d:DesignWidth="400">
8
9 <Grid x:Name="LayoutRoot" Background="Black" ShowGridLines="True" Margin="8">
10 <Grid.ColumnDefinitions>
11 <ColumnDefinition Width="196" />
12 <ColumnDefinition Width="*" />
13 </Grid.ColumnDefinitions>
14 <Grid.RowDefinitions>
15 <RowDefinition Height="*" />
16 <RowDefinition Height="48" />
17 </Grid.RowDefinitions>
18
19 <ListBox x:Name="myList"
20 HorizontalAlignment="Stretch"
21 VerticalAlignment="Stretch"
22 ItemsSource="{Binding}"
23 Grid.Row="0"
24 Grid.Column="0"
25 Grid.RowSpan="2"
26 SelectionChanged="myList_SelectionChanged">
27 <ListBox.ItemTemplate>
28 <DataTemplate>
29 <TextBlock Text="{Binding Name}" />
30 </DataTemplate>
31 </ListBox.ItemTemplate>
32 </ListBox>
33
34 <Image x:Name="myImage" Grid.Column="1" />
35 <Button Grid.Row="1"
36 Grid.Column="1"
37 Content="选择图片"
38 Margin="8" Click="Button_Click" FontSize="16" />
39 </Grid>
40 </UserControl>
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 mc:Ignorable="d"
7 d:DesignHeight="300" d:DesignWidth="400">
8
9 <Grid x:Name="LayoutRoot" Background="Black" ShowGridLines="True" Margin="8">
10 <Grid.ColumnDefinitions>
11 <ColumnDefinition Width="196" />
12 <ColumnDefinition Width="*" />
13 </Grid.ColumnDefinitions>
14 <Grid.RowDefinitions>
15 <RowDefinition Height="*" />
16 <RowDefinition Height="48" />
17 </Grid.RowDefinitions>
18
19 <ListBox x:Name="myList"
20 HorizontalAlignment="Stretch"
21 VerticalAlignment="Stretch"
22 ItemsSource="{Binding}"
23 Grid.Row="0"
24 Grid.Column="0"
25 Grid.RowSpan="2"
26 SelectionChanged="myList_SelectionChanged">
27 <ListBox.ItemTemplate>
28 <DataTemplate>
29 <TextBlock Text="{Binding Name}" />
30 </DataTemplate>
31 </ListBox.ItemTemplate>
32 </ListBox>
33
34 <Image x:Name="myImage" Grid.Column="1" />
35 <Button Grid.Row="1"
36 Grid.Column="1"
37 Content="选择图片"
38 Margin="8" Click="Button_Click" FontSize="16" />
39 </Grid>
40 </UserControl>
2. MainPage.xaml.cs 文件
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.IO;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Media.Animation;
13 using System.Windows.Shapes;
14
15 namespace BrowsePhotos
16 {
17 public partial class MainPage : UserControl
18 {
19 public MainPage()
20 {
21 InitializeComponent();
22 }
23
24 private void Button_Click(object sender, RoutedEventArgs e)
25 {
26 OpenFileDialog dlg = new OpenFileDialog()
27 {
28 Filter = "Jpeg Files (*.jpg)|*.jpg|All Files (*.*)|*.*",
29 Multiselect = true
30 };
31
32 if (true == dlg.ShowDialog())
33 {
34 myList.DataContext = dlg.Files;
35 }
36 }
37
38 private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
39 {
40 if ((e.AddedItems != null) && (e.AddedItems.Count > 0))
41 {
42 FileInfo fi = e.AddedItems[0] as FileInfo;
43 if (fi != null)
44 {
45 using (Stream stream = fi.OpenRead())
46 {
47 BitmapImage image = new BitmapImage();
48 image.SetSource(stream);
49 myImage.Source = image;
50 myImage.Visibility = Visibility.Visible;
51 stream.Close();
52 }
53 }
54 }
55 }
56 }
57 }
58
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.IO;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Media.Animation;
13 using System.Windows.Shapes;
14
15 namespace BrowsePhotos
16 {
17 public partial class MainPage : UserControl
18 {
19 public MainPage()
20 {
21 InitializeComponent();
22 }
23
24 private void Button_Click(object sender, RoutedEventArgs e)
25 {
26 OpenFileDialog dlg = new OpenFileDialog()
27 {
28 Filter = "Jpeg Files (*.jpg)|*.jpg|All Files (*.*)|*.*",
29 Multiselect = true
30 };
31
32 if (true == dlg.ShowDialog())
33 {
34 myList.DataContext = dlg.Files;
35 }
36 }
37
38 private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
39 {
40 if ((e.AddedItems != null) && (e.AddedItems.Count > 0))
41 {
42 FileInfo fi = e.AddedItems[0] as FileInfo;
43 if (fi != null)
44 {
45 using (Stream stream = fi.OpenRead())
46 {
47 BitmapImage image = new BitmapImage();
48 image.SetSource(stream);
49 myImage.Source = image;
50 myImage.Visibility = Visibility.Visible;
51 stream.Close();
52 }
53 }
54 }
55 }
56 }
57 }
58