日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图解使用Win8Api进行Metro风格的程序开发二----使用文件选择器访问和保存文件

發布時間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图解使用Win8Api进行Metro风格的程序开发二----使用文件选择器访问和保存文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們緊接著上篇,這篇將介紹如何使用文件選擇器訪問和保存文件

-----------------------------------我是華麗的分割線-----------------------------------------

此示例演示用戶如何使用文件選擇器選擇您的應用程序文件和文件夾,
根據用戶指定的名稱,文件類型和文件保存的位置。
這個示例使用Windows.Storage.Pickers API。

本篇將介紹如下四個方面:

a)讓用戶選擇一個文件

b)讓用戶選擇多個文件

c)讓用戶選擇一個文件夾

d)讓用戶保存文件和指定的名稱,文件類型和/或保存位置

我們的創建的步驟如下:

1)為了組織文件方便,我們先建一個文件夾FilePicker

2)向文件夾中添加如下四個文件:

  PickAFolder.xaml,PickASinglePhoto.xaml,PickMultipleFiles.xaml,SaveAFile.xaml

  創建方式如圖:

3)此時的解決方案結構如下:

4)向我們的DataSource添加導航所需要的信息

  修改我們的SampleDataSource.cs文件中的SampleDataSource類中的代碼,

  代碼如下: 

View Code public sealed class SampleDataSource{private static SampleDataSource _sampleDataSource = new SampleDataSource();private ObservableCollection<SampleDataGroup> _allGroups = new ObservableCollection<SampleDataGroup>();public ObservableCollection<SampleDataGroup> AllGroups{get { return this._allGroups; }}public static IEnumerable<SampleDataGroup> GetGroups(string uniqueId){if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");return _sampleDataSource.AllGroups;}public static SampleDataGroup GetGroup(string uniqueId){// Simple linear search is acceptable for small data setsvar matches = _sampleDataSource.AllGroups.Where((group) => group.UniqueId.Equals(uniqueId));if (matches.Count() == 1) return matches.First();return null;}public static SampleDataItem GetItem(string uniqueId){// Simple linear search is acceptable for small data setsvar matches = _sampleDataSource.AllGroups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));if (matches.Count() == 1) return matches.First();return null;}public SampleDataSource(){var group1 = new SampleDataGroup("FilePicker","Use Windows.Storage.Pickers API","Access and save files using the file picker","Assets/FilePicker.jpg","");group1.Items.Add(new SampleDataItem("FilePicker-PickASinglePhoto","Pick a single photo","only one file can selected,file type is jpg,jpeg,png","Assets/FilePicker.jpg","only one file can selected ","",group1,typeof(PickASinglePhoto)));group1.Items.Add(new SampleDataItem("FilePicker-PickMultipleFiles","Pick multiple files","you can pick multiple files","Assets/FilePicker.jpg","pick multiple files","",group1,typeof(PickMultipleFiles)));group1.Items.Add(new SampleDataItem("FilePicker-PickAFolder","Pick a folder","you can pick a folder","Assets/FilePicker.jpg","Pick a folder","",group1,typeof(PickAFolder)));group1.Items.Add(new SampleDataItem("FilePicker-SaveAFile","Save a file","you can save a file","Assets/FilePicker.jpg","Save a file","",group1,typeof(SaveAFile)));this.AllGroups.Add(group1);}}

?

5)我們的導航這樣就做好了,效果圖:

點擊,出現如下圖片:

6)我們開始我們的任務:讓用戶選擇一個文件

使用的FileOpenPicker的PickSingleFileAsync方法來調用一個文件選擇器窗口,
讓用戶選擇一個單一的文件。

修改PickASinglePhoto.xaml的代碼:

View Code <Grid x:Name="LayoutRoot" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" HorizontalAlignment="Left" VerticalAlignment="Top"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid x:Name="Input" Grid.Row="0"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" HorizontalAlignment="Left" >Prompt the user to pick a single photo.</TextBlock><Button Grid.Row="1" x:Name="PickAFileButton" Content="Pick photo" Margin="0,10,10,0"/></Grid><Grid x:Name="Output" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"><TextBlock x:Name="OutputTextBlock" Style="{StaticResource SubheaderTextStyle}" TextWrapping="Wrap" /></Grid></Grid>

?

修改后臺代碼:

View Code public sealed partial class PickASinglePhoto : Page{public PickASinglePhoto(){this.InitializeComponent();PickAFileButton.Click += new RoutedEventHandler(PickAFileButton_Click);}/// <summary>/// Invoked when this page is about to be displayed in a Frame./// </summary>/// <param name="e">Event data that describes how this page was reached. The Parameter/// property is typically used to configure the page.</param>protected override void OnNavigatedTo(NavigationEventArgs e){}private async void PickAFileButton_Click(object sender, RoutedEventArgs e){FileOpenPicker openPicker = new FileOpenPicker();//設置呈現視圖模式為縮略圖openPicker.ViewMode = PickerViewMode.Thumbnail;//設置文件選擇器打開時的起始位置,這里設置為圖片庫openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;//添加文件過濾器openPicker.FileTypeFilter.Add(".jpg");openPicker.FileTypeFilter.Add(".jpeg");openPicker.FileTypeFilter.Add(".png");//異步調用PickSingleFileAsyncStorageFile file = await openPicker.PickSingleFileAsync();if (file != null){OutputTextBlock.Text = "Picked photo: " + file.Name;}else{OutputTextBlock.Text = "Operation cancelled.";}}}

?

效果圖如下:

7)讓用戶選擇多個文件

  使用的FileOpenPicker的PickMultipleFilesAsync方法來調用一個文件選擇器窗口,
  讓用戶選擇多個文件。

  修改我們的PickMultipleFiles.xaml

  代碼如下:

View Code <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid x:Name="Input" Grid.Row="0"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><TextBlock Grid.Row="0" TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" HorizontalAlignment="Left" >Prompt the user to pick one or more files.</TextBlock><Button Grid.Row="1" x:Name="PickFilesButton" Content="Pick files" Margin="0,10,10,0"/></Grid><Grid x:Name="Output" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"><TextBlock x:Name="OutputTextBlock" Style="{StaticResource SubheaderTextStyle}" TextWrapping="Wrap" /></Grid></Grid>

  后臺代碼:

View Code public sealed partial class PickMultipleFiles : Page{public PickMultipleFiles(){this.InitializeComponent();PickFilesButton.Click += new RoutedEventHandler(PickFilesButton_Click);}/// <summary>/// Invoked when this page is about to be displayed in a Frame./// </summary>/// <param name="e">Event data that describes how this page was reached. The Parameter/// property is typically used to configure the page.</param>protected override void OnNavigatedTo(NavigationEventArgs e){}private async void PickFilesButton_Click(object sender, RoutedEventArgs e){FileOpenPicker openPicker = new FileOpenPicker();//設置呈現視圖模式為列表openPicker.ViewMode = PickerViewMode.List;//設置文件選擇器打開時的起始位置,這里設置為文檔庫openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;//不過濾任何類型openPicker.FileTypeFilter.Add("*");//調用PickMultipleFilesAsync獲得選擇文件列表IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();if (files.Count > 0){StringBuilder output = new StringBuilder("Picked files:\n");foreach (StorageFile file in files){output.Append(file.Name + "\n");}OutputTextBlock.Text = output.ToString();}else{OutputTextBlock.Text = "Operation cancelled.";}} }

?

效果圖:

8)讓用戶選擇一個文件夾

使用的FolderPicker的PickSingleFolderAsync方法來調用一個文件選擇器窗口,
讓用戶選擇一個文件夾。

修改我們的PickAFolder.xaml的xmal代碼:

View Code <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid x:Name="Input" Grid.Row="0"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><TextBlock Grid.Row="0" TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" HorizontalAlignment="Left" >Prompt the user to pick a folder so its contents can be accessed later.</TextBlock><Button Grid.Row="1" x:Name="PickFolderButton" Content="Pick folder" Margin="0,10,10,0"/></Grid><Grid x:Name="Output" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"><TextBlock x:Name="OutputTextBlock" Style="{StaticResource SubheaderTextStyle}" TextWrapping="Wrap" /></Grid></Grid>

?

修改后臺代碼:

View Code public sealed partial class PickAFolder : Page{public PickAFolder(){this.InitializeComponent();PickFolderButton.Click += new RoutedEventHandler(PickFolderButton_Click);}/// <summary>/// Invoked when this page is about to be displayed in a Frame./// </summary>/// <param name="e">Event data that describes how this page was reached. The Parameter/// property is typically used to configure the page.</param>protected override void OnNavigatedTo(NavigationEventArgs e){}private async void PickFolderButton_Click(object sender, RoutedEventArgs e){FolderPicker folderPicker = new FolderPicker();folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;folderPicker.FileTypeFilter.Add(".jpg");//調用PickSingleFolderAsync選擇文件夾StorageFolder folder = await folderPicker.PickSingleFolderAsync();if (folder != null){// 提供對列表的訪問,使用該列表,應用程序可以跟蹤最近訪問的文件或位置和將來要存儲的文件或位置StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);OutputTextBlock.Text = "Picked folder: " + folder.Name;}else{OutputTextBlock.Text = "Operation cancelled.";}}}

?

效果圖:

9)讓用戶保存文件和指定的名稱,文件類型和/或保存位置

使用的FileSavePicker的PickSaveFileAsync方法來調用一個文件選擇器窗口,
讓用戶保存文件。

修改我們的SaveAFile.xaml的代碼:

View Code <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid x:Name="Input" Grid.Row="0"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" HorizontalAlignment="Left" >Prompt the user to save a file.</TextBlock><Button Grid.Row="1" x:Name="SaveFileButton" Content="Save file" Margin="0,10,10,0"/></Grid><Grid x:Name="Output" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"><TextBlock x:Name="OutputTextBlock" Style="{StaticResource SubheaderTextStyle}" TextWrapping="Wrap" /></Grid></Grid>

?

修改后臺代碼:

View Code public sealed partial class SaveAFile : Page{public SaveAFile(){this.InitializeComponent();SaveFileButton.Click += new RoutedEventHandler(SaveFileButton_Click);}/// <summary>/// Invoked when this page is about to be displayed in a Frame./// </summary>/// <param name="e">Event data that describes how this page was reached. The Parameter/// property is typically used to configure the page.</param>protected override void OnNavigatedTo(NavigationEventArgs e){}private async void SaveFileButton_Click(object sender, RoutedEventArgs e){FileSavePicker savePicker = new FileSavePicker();savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;// 指定要保存的類型savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });// 默認的文件名savePicker.SuggestedFileName = "Refactor's blog";//調用保存文件方法StorageFile file = await savePicker.PickSaveFileAsync();if (file != null){// 阻止更新,直到我們完成更新文件,我們完成更新后調用CompleteUpdatesAsync方法 CachedFileManager.DeferUpdates(file);// 寫文件 await FileIO.WriteTextAsync(file, file.Name);// 讓系統知道我們正在完成更新文件,以便其他程序可以更新該文件FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);if (status == FileUpdateStatus.Complete){OutputTextBlock.Text = "File " + file.Name + " was saved.";}else{OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";}}else{OutputTextBlock.Text = "Operation cancelled.";}}}

?

效果圖就不發了!你懂的,圖太多了....

?

未完待續,敬請期待...

轉載請注明出處:http://www.cnblogs.com/refactor/

總結

以上是生活随笔為你收集整理的图解使用Win8Api进行Metro风格的程序开发二----使用文件选择器访问和保存文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产九九热 | 黄页网站免费在线观看 | 欧洲影院| 午夜一级在线 | 综合伊人久久 | 老色批av | 成人观看网站 | 法国极品成人h版 | 操韩国美女 | 深夜精品视频 | 欧美色狠 | 婷婷五月综合缴情在线视频 | 国产福利在线视频 | 精品国产乱码久久久久久免费 | 国产免费一区二区三区四区五区 | 日韩成人在线免费观看 | 一级全黄裸体免费观看视频 | 精品二区在线观看 | 操极品 | 亚洲国产中文字幕在线观看 | 色网站免费 | 瑟瑟在线视频 | 日韩精品一区二区三区电影 | 欧美午夜在线视频 | 亚洲天天操 | 97精品一区二区 | 国产a国产片| 一区二区三区视频在线播放 | 国产cao| 免费毛片观看 | 青青伊人精品 | 在线视频日韩精品 | 亚洲一区二区观看 | av免费看网站 | 午夜伊人网 | 国产综合婷婷 | 毛片综合 | ww久久 | 亚洲男人的天堂在线视频 | 中文在线观看免费高清 | 亚洲久久久久久 | 天堂视频免费 | 一区二区免费看 | 日本久久黄色 | 亚洲4区| 爱福利视频广场 | 国产模特av私拍大尺度 | 97香蕉超级碰碰久久免费软件 | a黄色片| 黄色专区 | 久久一区二区三区视频 | 国产成人无码精品久久久性色 | 精品人妻无码一区 | 国产av不卡一区 | 精品久久影视 | 青青草综合在线 | 青青国产精品视频 | 色婷婷av一区二区三区在线观看 | 九九热精品视频在线播放 | 成年人免费视频观看 | 国产五十路 | 国产性猛交xxxⅹ交酡全过程 | 亚洲福利影院 | 日韩亚洲在线 | 免费久久一级欧美特大黄 | 婷婷欧美 | 青草青草久热 | 欧美大尺度视频 | 农民人伦一区二区三区 | 手机av片 | 人妻熟女一区二区aⅴ水 | 又黄又爽又色的视频 | 日本高清视频一区二区三区 | 寻找身体恐怖电影免费播放 | 麻豆社| 国产欧美日韩在线播放 | 成人福利视频网 | 亚洲欧洲精品一区 | 青青草一区二区 | 欧美一区中文字幕 | 91看片看淫黄大片 | 国产一级免费大片 | 妞妞影视 | 911精品国产一区二区在线 | 日本va欧美va国产激情 | 亚洲av成人精品日韩在线播放 | 少妇一级淫片免费看 | 中文字幕亚洲激情 | 欧美老女人性视频 | 日韩手机在线观看 | 精品国产乱码久久久久久蜜臀网站 | 在线天堂中文在线资源网 | 粉嫩一区二区三区 | 黑人一级视频 | h片免费在线观看 | 夜夜嗨av一区二区三区网页 | 亚洲av无码专区国产乱码不卡 | 毛片www | 国产精品 欧美 日韩 |