【问题标题】:Open unsupported file type extensions with uwp app使用 uwp 应用程序打开不受支持的文件类型扩展名
【发布时间】:2020-08-30 22:03:07
【问题描述】:

This 是一个 uwp 文本编辑器应用程序的 PR,我正在实现桌面扩展,以便用户选择打开清单中未声明关联的文件。用户可以选择使用扩展名打开文件,然后使用 uwp 应用程序启动文件。但是我遇到了以下问题:

  1. StorageFile.GetFileFromPathAsync() 不适用于隐藏文件。尝试使用this 文章处理文件激活会出现以下错误:

    System.Exception HResult=0xD0000225 Message=与相关的文本 找不到这个错误代码。

    找不到与此错误代码相关的文本。

    Source=Windows.ApplicationModel StackTrace:在 Windows.ApplicationModel.AppInstance.GetActivatedEventArgs()

  2. 同样使用Launcher.LaunchFileAsync() 启动不适用于不受支持的扩展。

有关于这些问题的帮助吗?

【问题讨论】:

  • 我检查了上面的PR,但是它没有包含桌面扩展部分,你如何实现扩展?
  • 确实如此,桌面扩展的项目名称是 Notepads.DesktopExtension。去这里:github.com/soumyamahunt/Notepads/tree/…
  • 所以你调用launch api来启动带有桌面扩展文件的uwp应用程序,对吧?
  • 是的,但只有已支持的文件类型才能成功启动。对于其他文件类型,没有任何反应,也不例外。只是执行在该行停止。
  • 在我看来,这是设计使然,为什么要让您的应用支持所有文件类型?

标签: wpf uwp desktop-bridge


【解决方案1】:

在 Windows 10 2004(内部版本 19041)中,微软在包清单中引入了 uap10:FileType 属性,并通过提供 * 作为值,应用程序可以通过所有类型文件的菜单打开,甚至是没有任何扩展名的文件。要使用它,只需将以下代码添加到您的清单中:

xmlns:uap10="http://schemas.microsoft.com/appx/manifest/uap/windows10/10"
...
<uap:Extension Category="windows.fileTypeAssociation">
  <uap:FileTypeAssociation Name="anyfile">
    <uap:Logo>Assets\mylogo.png</uap:Logo>
    <uap:SupportedFileTypes>
      <uap:FileType>.txt</uap:FileType>
      <uap10:FileType>*</uap10:FileType>
    </uap:SupportedFileTypes>
  </uap:FileTypeAssociation>
</uap:Extension>

这仅适用于 Windows 10 build 19041 及更高版本。如果应用的目标最低版本低于 19041 以通过清单验证,则额外的 uap:FileType 是必需的,否则可以省略。

更新 1

对于早于 build 19041 的 Windows 10 版本,可以提供使用 IApplicationActivationManager::ActivateForFile 将文件激活重定向到 UWP 应用的桌面组件。 C++/WinRT 中的示例代码如下所示:

init_apartment();

LPWSTR* szArglist = NULL;
INT nArgs;
szArglist = CommandLineToArgvW(GetCommandLine(), &nArgs);
if (szArglist && nArgs > 1)
{
    INT ct = nArgs - 1;
    HRESULT hr = E_OUTOFMEMORY;
    com_ptr<IShellItemArray> ppsia = NULL;
    PIDLIST_ABSOLUTE* rgpidl = new(std::nothrow) PIDLIST_ABSOLUTE[ct];
    if (rgpidl)
    {
        hr = S_OK;
        INT cpidl;
        for (cpidl = 0; SUCCEEDED(hr) && cpidl < ct; cpidl++)
        {
            hr = SHParseDisplayName(szArglist[cpidl + 1], NULL, &rgpidl[cpidl], 0, NULL);
        }

        if (cpidl > 0 && SUCCEEDED(SHCreateShellItemArrayFromIDLists(cpidl, rgpidl, ppsia.put())))
        {
            com_ptr<IApplicationActivationManager> appActivationMgr = NULL;
            if (SUCCEEDED(CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_LOCAL_SERVER, __uuidof(appActivationMgr), appActivationMgr.put_void())))
            {
                DWORD pid = 0;
                appActivationMgr->ActivateForFile(AUMID, ppsia.get(), NULL, &pid);
            }
            appActivationMgr.~com_ptr();
        }

        for (INT i = 0; i < cpidl; i++)
        {
            CoTaskMemFree(rgpidl[i]);
        }
    }

    ppsia.~com_ptr();
    delete[] rgpidl;
}

LocalFree(szArglist);
uninit_apartment();

使用上述方法重定向激活的另一个好处是用户甚至可以直接使用 UWP 应用打开受限文件类型(即 .cmd、.bat 等)。

【讨论】:

    猜你喜欢
    • 2019-12-11
    • 2017-05-28
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多