[英]Show something other than, "All files" in a FileOpenPicker that isn't filtered to show all files
我正在編寫的 winrt 項目中通過 Windows::Storage::Pickers::FileOpenPicker 顯示打開文件對話框。 當我在選擇器上設置 FileTypeFilter 屬性時,它可以工作,但顯示的名稱仍然顯示“所有文件”。
我在 docs.microsoft.com 上看到 FileSavePicker 有一個 FileTypeChoices 屬性,該屬性包含一個 map 而不是一個允許保存選擇器為每種類型命名的向量,但我只能找到文件打開選擇器的 FileTypeFilter 向量。 即使是 Microsoft 示例的屏幕截圖也顯示“所有文件”,即使它已被過濾
我知道必須有某種方法可以做到這一點,因為我已經看到很多帶有能夠顯示名稱的文件打開選擇器的程序。
有誰知道如何讓打開的文件選擇器顯示默認的“所有文件”以外的內容?
注意:我已經做 c++ 幾年了,但是我對 c++/winrt 還是很陌生(就像本周早些時候剛剛了解了 winrt 一樣)所以我仍然不確定如何做很多基本的東西,比如以正確的方式設置文件選擇器
這是創建和打開對話框的代碼:
// Andrew Pratt 2021
// MainPage.cpp
#include "pch.h"
#include "MainPage.h"
#include "MainPage.g.cpp"
#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Storage.Pickers.h"
#include "winrt/Windows.Storage.Pickers.Provider.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Popups;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
namespace winrt::AltBrickUi::implementation
{
MainPage::MainPage()
{
InitializeComponent();
}
}
winrt::fire_and_forget winrt::AltBrickUi::implementation::MainPage::easyAlert(const IInspectable& title, const IInspectable& msg, const winrt::hstring& closeText)
{
ContentDialog alert{ ContentDialog() };
alert.Title(title);
alert.Content(msg);
alert.CloseButtonText(closeText);
alert.ShowAsync();
co_return;
}
void winrt::AltBrickUi::implementation::MainPage::MenuFlyoutItem_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
showJsonFileOpenPicker();
}
winrt::fire_and_forget winrt::AltBrickUi::implementation::MainPage::showJsonFileOpenPicker()
{
// Create file picker
Pickers::FileOpenPicker picker{};
picker.ViewMode(Pickers::PickerViewMode::List);
picker.FileTypeFilter().ReplaceAll({ winrt::to_hstring(L".json") });
// Open dialogue and get picked file
StorageFile pickedFile = co_await picker.PickSingleFileAsync();
if (pickedFile)
easyAlert(winrt::box_value(L"File picked!"), winrt::box_value(pickedFile.DisplayName()), L"Cool");
else
easyAlert(winrt::box_value(L"Nothing Picked"), NULL, L"Alright");
co_return;
}
您提到的情況總是發生在桌面應用程序中,例如 wpf 應用程序。 請閱讀此處了解更多信息。
我不得不說 uwp 沒有提供這樣的 api 來做到這一點。 如果您確實需要此功能,請使用 Windows 反饋中心應用程序提交您的功能要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.