【问题标题】:Show something other than, "All files" in a FileOpenPicker that isn't filtered to show all files在未过滤以显示所有文件的 FileOpenPicker 中显示除“所有文件”之外的其他内容
【发布时间】:2021-07-16 04:37:55
【问题描述】:

我正在编写的 winrt 项目中通过 Windows::Storage::Pickers::FileOpenPicker 显示打开文件对话框。当我在选择器上设置 FileTypeFilter 属性时,它可以工作,但显示的名称仍然显示“所有文件”。

我在 docs.microsoft.com 上看到 FileSavePicker 有一个 FileTypeChoices 属性,该属性包含一个映射而不是一个向量,允许保存选择器为每种类型命名,但我只能找到打开文件的 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;
}

这是我运行文件选择器时的外观:

显示我需要的方式的文件选择器示例(这个来自记事本):

【问题讨论】:

    标签: xaml uwp c++-winrt fileopenpicker


    【解决方案1】:

    您提到的情况总是发生在桌面应用程序中,例如wpf app。请阅读here 了解更多信息。

    我不得不说 uwp 没有提供这样的 api 来做到这一点。如果您确实需要此功能,请使用 Windows 反馈中心应用提交您的功能要求。

    【讨论】:

    • 是的,看起来 dialog.Filter 能够做到这一点。我可能会像你说的那样去反馈中心;与此同时,也许我可以采用稍微低级的解决方法,直到它成为 api 的一部分。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2013-10-09
    • 2011-05-27
    • 2017-04-03
    • 2010-11-12
    • 1970-01-01
    • 2020-02-01
    相关资源
    最近更新 更多