一共两个页面,第一个为MainPage.xaml,代码如下:
前台
后台代码如下:
MainPage.xaml.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using Microsoft.Phone.Controls; 13 using Microsoft.Phone.Tasks; 14 using Microsoft.Phone; 15 using System.IO; 16 using System.IO.IsolatedStorage; 17 18 namespace 在隔离存储中存取照片 19 { 20 public partial class MainPage : PhoneApplicationPage 21 { 22 // Constructor 23 string filename; 24 byte[] _imageAsByte; 25 public MainPage() 26 { 27 InitializeComponent(); 28 } 29 30 private void SaveButton_Click(object sender, RoutedEventArgs e) 31 { 32 filename = PhotoName.Text; 33 if (filename != ""&&PhotoImage.Source!=null) 34 { 35 using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 36 { 37 if (!store.FileExists(filename) || store.FileExists(filename) && MessageBox.Show("Overwrite the file?", "Question", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 38 { 39 using (var stream = store.CreateFile(filename)) 40 { 41 stream.Write(_imageAsByte, 0, _imageAsByte.Length); 42 } 43 PhotoImage.Source = null; 44 PhotoName.Text = ""; 45 NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); 46 } 47 } 48 } 49 else 50 { 51 MessageBox.Show("Sorry,but you must take a photo and write the photo‘s name in the textbox!","Warning!",MessageBoxButton.OK); 52 } 53 } 54 55 private void TakePhotoButton_Click(object sender, RoutedEventArgs e) 56 { 57 CameraCaptureTask task = new CameraCaptureTask(); 58 task.Completed += new EventHandler<PhotoResult>(task_Completed); 59 task.Show(); 60 } 61 62 void task_Completed(object sender, PhotoResult e) 63 { 64 if (e.TaskResult == TaskResult.OK) 65 { 66 _imageAsByte = new byte[e.ChosenPhoto.Length]; 67 e.ChosenPhoto.Read(_imageAsByte, 0, _imageAsByte.Length); 68 e.ChosenPhoto.Seek(0, SeekOrigin.Begin); 69 this.PhotoImage.Source = PictureDecoder.DecodeJpeg(e.ChosenPhoto); 70 } 71 } 72 73 private void ApplicationBarIconButton_Click(object sender, EventArgs e) 74 { 75 NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relative)); 76 } 77 } 78 }
效果如图所示:
同时还新建了一个照片文件的类,从IsolatedStorage里读取出照片及照片名再绑定到Page1.xaml中的ListBox中。类代码如下:
PhotoFile.cs 1 using System; 2 using System.Net; 3 using System.Windows; 4 using System.Windows.Controls; 5 using System.Windows.Documents; 6 using System.Windows.Ink; 7 using System.Windows.Input; 8 using System.Windows.Media; 9 using System.Windows.Media.Animation; 10 using System.Windows.Shapes; 11 12 namespace 在隔离存储中存取照片 13 { 14 public class PhotoFile 15 { 16 public ImageSource Image { get; set; } 17 public string ImageName { get; set; } 18 } 19 }
第二个Page1.xaml,代码如下:
1 <!--ContentPanel - place additional content here--> 2 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 3 <ListBox Height="550" HorizontalAlignment="Left" Margin="6,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="460"> 4 <ListBox.ItemTemplate> 5 <DataTemplate> 6 <Grid Margin="{StaticResource PhoneTouchTargetOverhang}"> 7 <StackPanel Orientation="Horizontal"> 8 <Image Source="{Binding Image}" Width="130" Height="120" Stretch="Fill"/> 9 <TextBlock Text="{Binding ImageName}" Foreground="Coral" FontSize="32" TextWrapping="Wrap" Width="300"/> 10 </StackPanel> 11 </Grid> 12 </DataTemplate> 13 </ListBox.ItemTemplate> 14 </ListBox> 15 </Grid> 16 </Grid> 17 18 <!--Sample code showing usage of ApplicationBar--> 19 <phone:PhoneApplicationPage.ApplicationBar> 20 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 21 <shell:ApplicationBarIconButton IconUri="/Images/appbar.add.rest.png" Text="添加" Click="ApplicationBarIconButton_Click"/> 22 </shell:ApplicationBar> 23 </phone:PhoneApplicationPage.ApplicationBar>
后台代码:
Page1.xaml.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using Microsoft.Phone.Controls; 13 using System.IO; 14 using System.IO.IsolatedStorage; 15 using System.Windows.Media.Imaging; 16 17 namespace 在隔离存储中存取照片 18 { 19 public partial class Page1 : PhoneApplicationPage 20 { 21 IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); 22 public Page1() 23 { 24 InitializeComponent(); 25 } 26 27 protected override void OnNavigatedTo(NavigationEventArgs e) 28 { 29 List<PhotoFile> filelist = new List<PhotoFile>(); 30 if (file.GetFileNames() != null) 31 { 32 foreach (string filename in file.GetFileNames()) 33 { 34 PhotoFile photo = new PhotoFile(); 35 BitmapImage bmp = new BitmapImage(); 36 using (IsolatedStorageFileStream filestream = file.OpenFile(filename, FileMode.Open, FileAccess.ReadWrite)) 37 { 38 bmp.SetSource(filestream); 39 filestream.Flush(); 40 filestream.Close(); 41 photo.Image = bmp; 42 photo.ImageName = " 照片名:"+filename; 43 } 44 filelist.Add(photo); 45 } 46 listBox1.ItemsSource = filelist; 47 } 48 } 49 50 private void ApplicationBarIconButton_Click(object sender, EventArgs e) 51 { 52 NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative)); 53 } 54 } 55 }
运行效果如下:
时间: 2024-10-17 07:33:31