The file selector in Windows 8 is significantly different from the previous versions of Windows. This article also explains how to use the Windows File selector for single-choice and multi-choice files, in addition, you also need to check the file storage and folder selector.
Before that, we need to understand the classes used by these three selectors in Windows 8 as follows:
File selector: fileopenpicker
Common attributes and Methods: suggestedstartlocation-the initial position of the selector
Commitbuttontext-text of the submit button
Filetypefilter-set the file type that can be selected
Viewmode-browsing mode: thumbnails and file lists
Picksinglefileasync ()-method for selecting a single file
Pickmultiplefilesasync ()-method for selecting multiple files
File savepicker
Common attributes and Methods: suggestedstartlocation-the initial position of the selector
Commitbuttontext-text of the submit button
Suggestedfilename-default file name saved
Defaultfileextension-default file type
Filetypechoices-set the file type combination that can be selected
Picksavefileasync ()-Method for saving files
File selector: folderopenpicker
Common attributes and Methods: suggestedstartlocation-the initial position of the selector
Commitbuttontext-text of the submit button
Filetypefilter-set the file type that can be selected
Viewmode-browsing mode: thumbnails and file lists
Picksinglefolderasync ()-method for selecting a single file
The following example shows how to use these three selectors:
XAML:
<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <button content = "single file selector" horizontalalignment = "Left" margin = "171,142, 192 "name =" btnsinglefile "verticalignment =" TOP "Height =" 55 "width =" "Click =" btnsinglefile_click "/> <button content =" Multi-file selector "horizontalalignment = "Left" margin = "501,142, 192 "name =" btnmultiplefile "verticalignment =" TOP "Height =" 55 "width =" "Click =" btnmultiplefile_click "/> <button content =" file saver "horizontalalignment = ""Left" margin = "171,250, 192 "name =" btnsavefile "verticalignment =" TOP "Height =" 55 "width =" "Click =" btnsavefile_click "/> <button content =" folder selector "horizontalalignment =" left "margin =" 501,250, 192 "name =" btnsinglefolder "verticalalignment =" TOP "Height =" 55 "width =" "Click =" btnsinglefolder_click "/> <textblock horizontalalignment =" Left "margin =" 1744, 522 "textwrapping =" Wrap "name =" tbmsg "text =" textblock "verticalignment =" TOP "Height =" 34 "width =" "fontsize =" 18 "/> </GRID>
XAML. CS:
Private async void btnsinglefile_click (Object sender, routedeventargs e) {fileopenpicker filepicker = new fileopenpicker (); // default Desktop Folder filepicker. suggestedstartlocation = pickerlocationid. desktop; // The display text filepicker of the submit button. commitbuttontext = ""; // specifies the filepicker file type that can be selected by the selector. filetypefilter. add (". TXT "); filepicker. filetypefilter. add (". jpg "); filepicker. filetypefilter. add (". PDF "); filepicker. filetypefilt Er. Add (". BMP"); // The browsing mode of the selector. filepicker. viewmode = pickerviewmode. thumbnail; storagefile file = await filepicker. picksinglefileasync (); If (file! = NULL) {This. tbmsg. TEXT = "you selected '" + file. name + "'file" ;}} private async void btnmultiplefile_click (Object sender, routedeventargs e) {fileopenpicker filepicker = new fileopenpicker (); filepicker. suggestedstartlocation = pickerlocationid. desktop; filepicker. filetypefilter. add (". TXT "); filepicker. filetypefilter. add (". jpg "); filepicker. filetypefilter. add (". PDF "); filepicker. filetypefilter. add (". BM P "); filepicker. viewmode = pickerviewmode. thumbnail; ireadonlylist <storagefile> filelist = await filepicker. pickmultiplefilesasync (); If (filelist! = NULL) {var filenamelist = string. empty; foreach (storagefile file in filelist) {filenamelist + = file. name + ",";} This. tbmsg. TEXT = "you have selected '" + filenamelist + "' and other files";} private async void btnsavefile_click (Object sender, routedeventargs e) {filesavepicker savepicker = new filesavepicker (); savepicker. suggestedstartlocation = pickerlocationid. desktop; savepicker. commitbuttontext = "Save this file"; s Avepicker. defaultfileextension = ". jpg "; savepicker. filetypechoices. add ("TXT file", new list <string> (){". TXT "}); savepicker. filetypechoices. add ("Photo View", new list <string> (){". jpg ",". PDF "}); savepicker. suggestedfilename = "default file name"; storagefile file = await savepicker. picksavefileasync (); If (file! = NULL) {cachedfilemanager. deferupdates (File); await fileio. writetextasync (file, "This is what we learned today, storage location! "); This. tbmsg. TEXT = "You saved '" + file. name + "'file"; fileupdatestatus status = await cachedfilemanager. completeupdatesasync (File) ;}}// private async void btnsinglefolder_click (Object sender, routedeventargs e) {folderpicker = new folderpicker (); folderpicker. suggestedstartlocation = pickerlocationid. desktop; folderpicker. commitbuttontext = "Confirm folder"; folderpicker. filetypefilter. A Dd (". TXT "); folderpicker. filetypefilter. add (". jpg "); folderpicker. filetypefilter. add (". PDF "); storagefolder folder = await folderpicker. picksinglefolderasync (); If (folder! = NULL) {storageapplicationpermissions. futureaccesslist. addorreplace ("Default Folder", folder); this. tbmsg. TEXT = "you selected '" + folder. name + "'Folder ";}}
In addition, double-click the package. appxmanifest file in the project and select the declaration --> "file selector" and "file saver" --> both of them need to select "any available types are supported", for example:
The final effect is as follows. If you need the source code, click win8picker.rar to download it: