"Go" Windows phone to access image files in isolated storage

Source: Internet
Author: User

A total of two pages, the first is MainPage.xaml, the code is as follows:

Front desk

The background code is as follows:

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;1718 namespace access photos in isolated storage {public partial class Mainpage:phoneapplicationpage21 {         $//Constructor23 string filename;24 byte[] _imageasbyte;25 public MainPage () 26 {InitializeComponent ();}2930 private void saveButton_Click (object sender, Routedeventarg S e) {+ filename = photoname.text;33 if (filename! = "" &&photoimage.source!=nul L) {using (var store = IsolateDstoragefile.getuserstoreforapplication ()) (!store. FileExists (filename) | | Store. FileExists (filename) && MessageBox.Show ("Overwrite the file?", "Question", messageboxbutton.okcancel) = = Messageboxresult.ok) (var stream = store). CreateFile (filename). Write (_imageasbyte, 0, _imageasbyte.length),}43 Photoimage.source = nul l;44 photoname.text = ""; Navigationservice.navigate (New Uri ("/page1.xam                 L ", urikind.relative));}47}48}49 Else50 {51 MessageBox.Show ("Sorry,but You must take a photo and write the photo's name in the textbox!", "warning!", Mes Sageboxbutton.ok);}53}5455 private void Takephotobutton_cLick (object sender, RoutedEventArgs e). {Cameracapturetask task = new Cameracapturetask (); 58 task.completed + = new eventhandler<photoresult> (task_completed); Show ();}6162 void Task_completed (object sender, Photoresult e) (E.taskresu lt = = Taskresult.ok) (_imageasbyte = new byte[e.chosenphoto.length];67 E.)                 Chosenphoto.read (_imageasbyte, 0, _imageasbyte.length); E.chosenphoto.seek (0, Seekorigin.begin); 69 This. Photoimage.source = Picturedecoder.decodejpeg (E.chosenphoto);}71}7273 private void Applica Tionbariconbutton_click (object sender, EventArgs e) The Navigationservice.navigate (New Uri ("/page1.x AML ", urikind.relative)); 76}77}78}

Effect:

It also creates a new class of photo files, reads and removes photos and photo names from IsolatedStorage and binds them to the listbox in Page1.xaml. The class code is as follows:

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;1112 namespace access photos in isolated storage     PhotoFile15     {public         imagesource Image {get; set;} + Public         string ImageName {get; set;}     }19}

The second Page1.xaml, the code is as follows:

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" Vertica  Lalignment= "Top" width= "460" > 4 <ListBox.ItemTemplate> 5 <DataTemplate> 6 <grid margin= "{StaticResource Phonetouchtargetoverhang}" > 7 & Lt StackPanel orientation= "Horizontal" > 8 <image source= "{Binding Image}" width= "" "H eight= "stretch=" "Fill"/> 9 <textblock text= "{Binding ImageName}" foreground= "Cor                         Al "Fontsize=" "textwrapping=" Wrap "width="/>10 </stackpanel>11             </grid>12 </datatemplate>13 </listbox.itemtemplate>14 </listbox>15 </grid>16 </grid>17 <!--Sample code showing usage of applicationbar-->19 &lt ;p hone:phoneapplicationpage.applicationbar>20 <shell:applicationbar isvisible= "true" IsMenuEnabled= "true" &G t;21 <shell:applicationbariconbutton iconuri= "/images/appbar.add.rest.png" text= "Add" Click= "ApplicationBarI" Conbutton_click "/>22 </shell:applicationbar>23 </phone:PhoneApplicationPage.ApplicationBar>

Background code:

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 ; 1617 namespace to access photos in isolated storage. {public partial class Page1:phoneapplicationpage20 {Isolatedstoragefi Le file = isolatedstoragefile.getuserstoreforapplication (); public Page1 () @ Initializec             Omponent ();}2627 protected override void Onnavigatedto (NavigationEventArgs e) 28 {29 list<photofile> filelist = new list<photofile> (), and if (file. GetFileNames () = null)-{+-foreach (string filename in file. GetfiLenames ()) {Photofile photo = new Photofile (); BitmapImage BMP = new BitmapImage (); using (IsolatedStorageFileStream filestream = file. OpenFile (filename, FileMode.Open, fileaccess.readwrite)) the PNS {BMP. SetSource (FileStream); FileStream. Flush (); FileStream. Close (); photo. Image = bmp;42 photo. ImageName = "Photo name:" +filename;43}44 filelist.         ADD (photo),}46 listbox1.itemssource = filelist;47}48}4950 private void Applicationbariconbutton_click (object sender, EventArgs e) {Navigationservice.navig Ate (New Uri ("/mainpage.xaml", urikind.relative)); 53}54}55}

The results are as follows:

"Go" Windows phone to access image files in isolated storage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.