標籤:style blog color os io 檔案 ar div cti
上一篇文章小夢分享了檔案選取器FileOpenPicker的用法,這篇文章我們繼續分享FileSavePicker的用法,FileSavePicker的用法幾乎和FileOpenPicker用法一模一樣.唯一的區別就是在FileOpenPicker中是通過FileTypeFilter屬性添家字串元素的,但是在FileSavePicker中是向FileTypeChoices屬性工作表示的集合中添加元素的.FileTypeChoices是字典類型,因此它的每一個元素都必須具有唯一的鍵名,對應的值是IList<string>類型.
下面我們通過將textbox的內容存入一個txt檔案來說明FileSavePicker的用法,過程和FileOpenPicker用法是一樣的.
就把代碼依次貼出來了:
private void saveButton_Click(object sender, RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.FileTypeChoices.Add("TXT", new List<string>() { ".txt" }); savePicker.SuggestedFileName = "小夢"; savePicker.ContinuationData["Operation"] = "Save"; savePicker.PickSaveFileAndContinue(); }
protected override void OnActivated(IActivatedEventArgs args) { if (args is FileSavePickerContinuationEventArgs) { Frame rootFrame = Window.Current.Content as Frame; if (!rootFrame.Navigate(typeof(MainPage))) { throw new Exception("Failed to create target page"); } var s = rootFrame.Content as MainPage; s.SavePickerArgs = (FileSavePickerContinuationEventArgs)args; } Window.Current.Activate(); }
private FileSavePickerContinuationEventArgs savePickerArgs; public FileSavePickerContinuationEventArgs SavePickerArgs { get { return savePickerArgs; } set { savePickerArgs = value; ContinuFileSavePicker(savePickerArgs); } } private async void ContinuFileSavePicker(FileSavePickerContinuationEventArgs args) { if (args.ContinuationData["Operation"] as string == "Save" && args.File != null) { StorageFile txtFile = args.File; await FileIO.WriteTextAsync(txtFile, this.text.Text.ToString(), Windows.Storage.Streams.UnicodeEncoding.Utf8); } }
windows phone 8.1開發:檔案選取器FileSavePicker