New Fashion Windows 8 Development (12): How to select and open a file

Source: Internet
Author: User

In this blog, http://blog.csdn.net/tcjiaan.

 

In the winform era, we cannot forget openfiledialog. So what components are similar in Windows Store applications?

It is windows. Storage. pickers. fileopenpicker. In fact, we can also know its purpose from the class name. It seems that it is necessary to learn a few words.

 

Fileopenpicker can select a single file (single choice) or multiple files (Multiple choices). After the selected file is submitted, it returns a windows. storage. storagefile. You can think of this class as a file. We will know it later, and now we will sell it first.

Then, how can I perform this operation? Actually, learning a new thing is nothing more intuitive than an instance in the world.

 

Now, follow my steps and try again. (In what language do you like, I use CS)

1. Start vs for Win8 and create a blank page app.

2. Put a button and an image in mainpage. XAML of the home page.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition/> </grid. rowdefinitions> <stackpanel orientation = "horizontal" grid. row = "0"> <button margin = "6, 1, "content =" select a file "Click =" onselectfile "/> <textblock name =" displaypath "margin =, 0, 5 "fontsize =" 18 "/> </stackpanel> <image grid. row = "1" name = "IMG" stretch = "Uniform" margin = "15"/> </GRID>

3. Open the code file mainpage. XAML. cs. The Code is as follows.

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. storage; using Windows. storage. pick ERS; using Windows. storage. streams; namespace openfilepickersample {public sealed partial class mainpage: Page {public mainpage () {This. initializecomponent ();} private async void onselectfile (Object sender, routedeventargs e) {fileopenpicker picker = new fileopenpicker (); // instantiate a fileopenpicker, such as a picker. filetypefilter. add (". jpg "); picker. filetypefilter. add (". JPEG "); // set the initial Path picker. Encoding = pickerlocationid. pictureslibrary; // pickmultiplefilesasync is used to select multiple files. // picksinglefileasync selects a single file storagefile file = await picker. picksinglefileasync (); If (file! = NULL) {This. displaypath. TEXT = file. path; irandomaccessstream stream = await file. openasync (fileaccessmode. read); // create a new bitmap for Windows. UI. XAML. media. imaging. bitmapimage BMP = new windows. UI. XAML. media. imaging. bitmapimage (); BMP. setsource (Stream); // display the image this. IMG. source = BMP ;}}}}

The suggestedstartlocation attribute indicates the directory in which content is displayed when the file selector is enabled. It is an enumeration value.

To select a single file, call picksinglefileasync. To select multiple files, call the pickmultiplefilesasync method. Both methods are executed asynchronously. Therefore, you need to add the await keyword.

Press F5 to run the application, click the "select a file" button, select an image, and click OK. The page displays the image we just selected.

 

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.