尝试从WebView ScriptNotify打开文件选择器时,即使由Dispatcher运行,UWP应用程序也会崩溃

问题描述

Reproducible repo here

我的应用程序具有用于UI的WebView,它使用window.external.notify通知后端代码:

        public MainPage()
        {
            this.InitializeComponent();

            this.web.ScriptNotify += this.Web_ScriptNotify;
            this.web.Navigate(new Uri("ms-appx-web:///index.html"));
        }

        private void Web_ScriptNotify(object sender,NotifyEventArgs e)
        {
            _ = this.Dispatcher.RunAsync(
                Windows.UI.Core.CoreDispatcherPriority.Normal,async () =>
            {
                var d = new FileOpenPicker();

                // Changing UI text is okay
                this.lbl.Text = "Hello!";

                // Crash here:
                var file = await d.PickSingleFileAsync();
            });
        }

我要执行的命令之一是打开文件选择器。但是,它崩溃并带有此异常System.Runtime.InteropServices.COMException: 'Unspecified error'。我以为是因为我试图从外部UI线程中打开它,所以我已经在Dispatcher中运行了它,但是它仍然会发生。

enter image description here

我该怎么办?

解决方法

它崩溃是因为我没有设置FileTypeFilter,不是因为该事件。添加此功能有助于:

diagOpen.FileTypeFilter.Add("*");

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...