日韩性视频-久久久蜜桃-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风格的程序开发二----使用文件选择器访问和保存文件的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 野花视频免费在线观看 | 亚洲大片精品 | 国产成人亚洲精品无码h在线 | 麻豆视频一区二区 | 国产精品亚洲第一 | 热久久伊人 | 老熟妇高潮一区二区高清视频 | 国产午夜精品免费一区二区三区视频 | 人人爽视频 | 欧美黑人巨大xxx极品 | 特级西西444www高清大胆 | 亚洲熟女综合一区二区三区 | 国产九九九精品 | 成人欧美一区二区三区黑人动态图 | 神马久久网站 | 69av一区二区三区 | 精品一区二区三区四区五区六区 | 色欲av无码一区二区三区 | 最近最经典中文mv字幕 | 天天狠狠干 | xxxxx在线视频| 国产女厕一区二区三区在线视 | 一区二区激情视频 | 黄网免费在线观看 | 黄色变态网站 | 天堂婷婷| 国产精品666 | 久久国产激情 | 一区二区不卡免费视频 | 欧美日韩一区二区三区在线播放 | 亚洲综合av一区二区三区 | 久久精品福利 | 欧美日韩三级 | 国产自偷自拍视频 | 国产免费观看视频 | 欧美午夜性 | 国产精品久久综合青草亚洲AV | 色婷婷综合网 | 99久草 | 欲色综合| 羞羞色院91蜜桃 | 91精品国产一区二区三区蜜臀 | 日韩视频中文字幕 | 日韩欧美色 | 日本二三区 | 高清日韩欧美 | 一级特黄色片 | 黄色片视频免费看 | 国产无码精品视频 | 免费人成又黄又爽又色 | 毛片视频网站在线观看 | 精品国产乱码久久久久久浪潮 | 99久久精品国产色欲 | 亚洲福利网 | 国产精品又黄又爽又色无遮挡 | 被扒开腿一边憋尿一边惩罚 | 久久精品国产亚洲AV高清综合 | 中文字幕第一页在线视频 | 亚洲一区二区三区影视 | 日韩一区二区免费播放 | 少妇特黄一区二区三区 | 亚洲视频黄色 | 在线观看波多野结衣 | 国产在线不卡一区 | 538国产视频| 天堂中文资源在线 | 黄色h视频 | 林天顾悦瑶笔趣阁 | 黄色成人小视频 | 欧美人体视频一区二区三区 | 五月天堂婷婷 | 伊人网在线观看 | 欧美日韩在线影院 | 久久99久久99精品免观看粉嫩 | 中文字幕成人av | 黄色小说在线观看视频 | 四虎影视免费永久大全 | 麻豆伊甸园 | 香蕉综合在线 | 黑白配高清国语在线观看 | 九九九在线观看 | 嫩草国产精品 | 老司机免费在线视频 | 中文字幕999| 色www.| 国精品一区 | 国产女主播在线播放 | 久久久久黄色 | 风间由美一区二区 | 久久久老司机 | 韩国三级hd中文字幕 | 亚洲视频免费在线 | 成人毛片18女人毛片免费 | 久久涩综合 | 一级片a级片 | 性爱免费视频 | 午夜免费福利网站 | eeuss鲁片一区二区三区在线观看 | 可以直接看av的网址 |