Old Zhou's blogHttp://blog.csdn.net/tcjiaan, Reprinted please indicate the original author and source.
PreviousArticleWe have discussed opening the file UI. Now, we will continue to explore the UI components for saving the file. The same principle is also very simple.
This time, we use the windows. Storage. pickers. filesavepicker class, which is basically the same as how to use the last opened file. After the picksavefileasync method is called, if the user confirms rather than cancels the operation, a storagefile instance is returned, and the write operation can be expanded around the storagefile object.
Let's use an example.
Step 2: Start vs to create a Windows Store application on a blank page.Program.
Step 2: Layout mainpage. XAML on the home page as follows.
<Page X: class = "savefilepickerexample. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Local =" using: savefilepickerexample "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: MC =" http://schemas.openxmlformats.org/markup-compatibility/2006 "MC: ignorable = "D"> <canvas background = "{staticresource appbarbackgroundthemebrush}"> <textbox name = "txtcontent" canvas. left = "6" canvas. top = "11" width = "1279" Height = "365" textwrapping = "Wrap" fontsize = "18"/> <button content = "Save to file" canvas. left = "54" fontsize = "24" canvas. top = "414" padding = "" Click = "btnsave_click"/> <textblock name = "MSG" fontsize = "20" canvas. left = "280" canvas. top = "450"/> </canvas> </Page>
Step 2: SwitchCodeView to complete the following code.
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; namespace savefilepickerexample {// <summary> /// you can use it to itself or navigate to a blank page inside the frame. /// </Summary> Public sealed partial class mainpage: Page {public mainpage () {This. initializecomponent ();} private async void btnsave_click (Object sender, routedeventargs e) {If (this.txt content. text. equals (string. empty) {return;} filesavepicker picker = new filesavepicker (); // The text picker displayed on the submit button. commitbuttontext = "save"; // supported file type picker. filetypechoices. add ("text file", new string [] {". TXT "} ); // Default display directory picker. suggestedstartlocation = pickerlocationid. desktop; // display the UI and return the storagefile file = await picker. picksavefileasync (); // write content to the file if (file! = NULL) {await fileio. writetextasync (file, txtcontent. Text, windows. Storage. Streams. unicodeencoding. utf8); this. msg. Text = "the file is saved. ";}}}}
Setting the commitbuttontext attribute is the text displayed on the submit button. For example, if we set "save" in the code above, the result is shown in.
The filetypechoices attribute setting supports the file type. It is a dictionary set. The key is the short text used for description. The value is a list value and can contain multiple extensions. For example:
JPEG image ---->. jpg. JPEG
Audio File ---->. MP3. wav. wma
The execution result of this example is shown in the figure below.
After the file is saved, you can use the note to open the file you just saved and view the corresponding text.