首页 > 代码库 > windows phone 8.1开发:文件选择器FileSavePicker

windows phone 8.1开发:文件选择器FileSavePicker

上一篇文章小梦分享了文件选择器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