• 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);
    
                }
    
            }
  • 相关阅读:
    洛谷P1656 炸铁路 题解 并查集
    洛谷P1455 搭配购买 题解 并查集+01背包
    欧拉计划第16题题解
    欧拉计划第15题题解
    欧拉计划第14题题解
    scrapy 爬虫保存数据
    FileNotFoundError: File b'score.xlsx' does not exist
    图片截取
    图像过滤器
    如何查看字符编码?
  • 原文地址:https://www.cnblogs.com/xdoudou/p/3936954.html
Copyright © 2020-2023  润新知