简体   繁体   English

在未过滤以显示所有文件的 FileOpenPicker 中显示除“所有文件”之外的其他内容

[英]Show something other than, "All files" in a FileOpenPicker that isn't filtered to show all files

I'm displaying an open file dialog via Windows::Storage::Pickers::FileOpenPicker in a winrt project I'm writing.我正在编写的 winrt 项目中通过 Windows::Storage::Pickers::FileOpenPicker 显示打开文件对话框。 When I set the FileTypeFilter property on the picker it works, but the name shown still says, "All files."当我在选择器上设置 FileTypeFilter 属性时,它可以工作,但显示的名称仍然显示“所有文件”。

I've seen on docs.microsoft.com that FileSavePicker has a FileTypeChoices property that holds a map instead of a vector allowing the save picker to have names for each type, but I only can find the FileTypeFilter vector for the file open picker.我在 docs.microsoft.com 上看到 FileSavePicker 有一个 FileTypeChoices 属性,该属性包含一个 map 而不是一个允许保存选择器为每种类型命名的向量,但我只能找到文件打开选择器的 FileTypeFilter 向量。 Even the screenshots for Microsoft's examples say "All files" even though it's filtered即使是 Microsoft 示例的屏幕截图也显示“所有文件”,即使它已被过滤

I know there has to be some way to do it, because I've seen plenty of programs with file open pickers that are able to display names.我知道必须有某种方法可以做到这一点,因为我已经看到很多带有能够显示名称的文件打开选择器的程序。


Does anyone know how to get the open file picker to show something other than the default "All files"?有谁知道如何让打开的文件选择器显示默认的“所有文件”以外的内容?

NOTE: I've been doing c++ for a few years, but I am still very new to c++/winrt (as in like, just learned what winrt was earlier this week) so I'm still not sure how to do a lot of basic things, like set up a file picker the right way注意:我已经做 c++ 几年了,但是我对 c++/winrt 还是很陌生(就像本周早些时候刚刚了解了 winrt 一样)所以我仍然不确定如何做很多基本的东西,比如以正确的方式设置文件选择器

This is the code that creates and opens the dialog:这是创建和打开对话框的代码:

// 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;
}

Here's a snippet of what the file picker looks like when I run it:这是我运行文件选择器时的一个片段: 在此处输入图像描述

An example of a file picker that displays the way I need it to (this one is from Notepad):显示我需要的方式的文件选择器示例(这个来自记事本): 在此处输入图像描述

The situation you mentioned always happens in desktop application, such as wpf app.您提到的情况总是发生在桌面应用程序中,例如 wpf 应用程序。 please read here to know more.请阅读此处了解更多信息。

I have to say that uwp doesn't provide such api to do this.我不得不说 uwp 没有提供这样的 api 来做到这一点。 If you do want this feature, please submit your feature requirement with Windows Feedback Hub app.如果您确实需要此功能,请使用 Windows 反馈中心应用程序提交您的功能要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM