Metro style app 文件、文件夹的选择、文件的保存。

 

 Metro style app 文件、文件夹的选择、文件的保存。

 

1、选择单个文件

        public async void PickAFile()
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                // Application now has read/write access to the picked file
                msg = "Picked photo: " + file.Name;
            }
            else
            {
                msg = "Operation cancelled.";
            }
        }

 

 

 

2、选择多个文件

        public async void PickFiles()
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add("*");
            IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
            if (files.Count > 0)
            {
                StringBuilder output = new StringBuilder("Picked files:\n");
                // Application now has read/write access to the picked file(s)
                foreach (StorageFile file in files)
                {
                    output.Append(file.Name + "\n");
                }
                msg = output.ToString();
            }
        }

 

3、选择文件夹

        public async void PickFolder()
        {
            FolderPicker folderPicker = new FolderPicker();
            folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            folderPicker.FileTypeFilter.Add(".docx");
            folderPicker.FileTypeFilter.Add(".xlsx");
            folderPicker.FileTypeFilter.Add(".pptx");
            folderPicker.FileTypeFilter.Add(".jpg");
            folderPicker.FileTypeFilter.Add(".jpeg");
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
                // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
                StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
                msg = "Picked folder: " + folder.Name;
            }
            else
            {
                msg = "Operation cancelled.";
            }

        }

 

 

4、保存文件

        public async void SaveFile()
        {
            FileSavePicker savePicker = new FileSavePicker();
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            // Dropdown of file types the user can save the file as
            savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
            // Default file name if the user does not type one in or select a file to replace
            savePicker.SuggestedFileName = "New Document";
            StorageFile file = await savePicker.PickSaveFileAsync();
            if (file != null)
            {
                // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
                CachedFileManager.DeferUpdates(file);
                // write to file
                await FileIO.WriteTextAsync(file, file.Name);
                // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
                // Completing updates may require Windows to ask for user input.
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                if (status == FileUpdateStatus.Complete)
                {
                    msg = "File " + file.Name + " was saved.";
                }
                else
                {
                    msg = "File " + file.Name + " couldn't be saved.";
                }
            }
        }

 

下面是C++版本

1、选择单个文件 

void BasicHandleCPP::MainPage::PickerAFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	FileOpenPicker^ openPicker = ref new FileOpenPicker();
	openPicker->ViewMode = PickerViewMode::Thumbnail;
	openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
	openPicker->FileTypeFilter->Append(".jpg");
	openPicker->FileTypeFilter->Append(".jpeg");
	openPicker->FileTypeFilter->Append(".png");

	create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file)
	{
		if (file)
		{
			Display->Text = "Picked photo: " + file->Name;
		}
		else
		{
			Display->Text = "Operation cancelled.";
		}
	});

}

 

 

2、选择多个文件

void BasicHandleCPP::MainPage::PickerFiles_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	FileOpenPicker^ openPicker = ref new FileOpenPicker();
	openPicker->ViewMode = PickerViewMode::List;
	openPicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;
	openPicker->FileTypeFilter->Append("*");

	create_task(openPicker->PickMultipleFilesAsync()).then([this](IVectorView<StorageFile^>^ files)
	{
		if (files->Size > 0)
		{
			String^ output = "Picked files:\n";
			std::for_each(begin(files), end(files), [this, &output](StorageFile ^file)
			{
				output += file->Name + "\n";
			});
			Display->Text = output;
		}
		else
		{
			Display->Text = "Operation cancelled.";
		}
	});
}

 

 

3、选择文件夹

void BasicHandleCPP::MainPage::PickerFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	FolderPicker^ folderPicker = ref new FolderPicker();
	folderPicker->SuggestedStartLocation = PickerLocationId::Desktop;

	// Users expect to have a filtered view of their folders depending on the scenario.
	// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
	folderPicker->FileTypeFilter->Append(".docx");
	folderPicker->FileTypeFilter->Append(".xlsx");
	folderPicker->FileTypeFilter->Append(".pptx");

	create_task(folderPicker->PickSingleFolderAsync()).then([this](StorageFolder^ folder)
	{
		if (folder)
		{
			Display->Text = "Picked folder: " + folder->Name;
		}
		else
		{
			Display->Text = "Operation cancelled.";
		}
	});
}

 

4、保存文件

void BasicHandleCPP::MainPage::SaveFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	FileSavePicker^ savePicker = ref new FileSavePicker();
	savePicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;

	auto plainTextExtensions = ref new Platform::Collections::Vector<String^>();
	plainTextExtensions->Append(".txt");
	savePicker->FileTypeChoices->Insert("Plain Text", plainTextExtensions);
	savePicker->SuggestedFileName = "New Document";

	create_task(savePicker->PickSaveFileAsync()).then([this](StorageFile^ file)
	{
		if (file != nullptr)
		{

			// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
			CachedFileManager::DeferUpdates(file);
			// write to file
			create_task(FileIO::WriteTextAsync(file, file->Name)).then([this, file]()
			{
				// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
				// Completing updates may require Windows to ask for user input.
				create_task(CachedFileManager::CompleteUpdatesAsync(file)).then([this, file](FileUpdateStatus status)
				{
					if (status == FileUpdateStatus::Complete)
					{
						Display->Text = "File " + file->Name + " was saved.";
					}
					else
					{
						Display->Text = "File " + file->Name + " couldn't be saved.";
					}
				});
			});
		}
		else
		{
			Display->Text = "Operation cancelled.";
		}
	});
}

 

 

 

 

 

 

 

 

 

原文链接: https://www.cnblogs.com/linlf03/archive/2012/06/28/2567009.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Metro style app 文件、文件夹的选择、文件的保存。

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/53852

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午5:12
下一篇 2023年2月9日 上午5:12

相关推荐