Search code examples
c#.netwebviewuwpfilepicker

UWP app crash when trying to open File Picker from WebView ScriptNotify even when running by Dispatcher


Reproducible repo here

My app has a WebView for UI and it notifies the backend code by using 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();
            });
        }

One of the command I want to do is to open a File Picker. However, it crashes with this exception System.Runtime.InteropServices.COMException: 'Unspecified error'. I thought it is because I tried to open it from outside UI thread, so I run it in a Dispatcher already but it still happens.

enter image description here

What should I do?


Solution

  • It crashes because I didn't set FileTypeFilter, not because of the event. Adding this helps:

    diagOpen.FileTypeFilter.Add("*");