简体   繁体   English

WPF WebView2 错误 - 源属性不能设置为 null

[英]WPF WebView2 Error - source property cannot be set to null

To authenticate an API service, it is required for the user to sign into a webpage.要验证 API 服务,用户需要登录网页。 If the sign-in is valid, an authentication code is appended to the end of a redirected Uri.如果登录有效,则身份验证代码将附加到重定向的 Uri 的末尾。 The authentication code is then used in the oauth to get the key, etc... To faciliate this, I have implemented WebView2:然后在 oauth 中使用身份验证代码来获取密钥等......为了方便起见,我已经实现了 WebView2:

        <wv2:WebView2 Name="webView"
                  Width="800"
                  Height="500"
                  Source="{Binding WebUri, Mode=TwoWay}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SourceChanged">
                <i:InvokeCommandAction Command="{Binding SourceChangedCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </wv2:WebView2>

I have validation logic in the viewmodel to recognize when the redirect uri contains the authentication code;我在视图模型中有验证逻辑来识别重定向 uri 何时包含身份验证代码; this is done through the SourceChangedCommand that is called when the SourceChanged event is thrown on the WebView (following the navigation event lifecycle described here ).这是通过在SourceChangedCommand上引发 SourceChanged 事件时调用的 SourceChangedCommand 完成的(遵循此处描述的导航事件生命周期)。 The SourceChangedCommand.Execute is as follows: SourceChangedCommand.Execute如下:

    public void Execute(object? parameter)
    {
        if (ValidateUri(_viewModel.WebUri.ToString()))
        {
            var str = _viewModel.WebUri.ToString().Substring(50);
            _navigationService.Authenticate(str);
        }
    }

Here is where I am running into a problem: I also have a NavigationService which navigates between viewmodels and changes view by datatemplates, etc... When navigating away from the view with the WebView2 (ie: changing data contexts on the parent view), I am given the following error:这是我遇到问题的地方:我还有一个 NavigationService,它在视图模型之间导航并通过数据模板等更改视图......当使用 WebView2 离开视图时(即:更改父视图上的数据上下文),我收到以下错误:

The Source property cannot be set to null Source 属性不能设置为 null

在此处输入图像描述

This is extremely confusing as the source for the WebView2 is never actually set to null - it is maintained as the redirected Uri.这非常令人困惑,因为 WebView2 的源实际上从未设置为 null - 它被维护为重定向的 Uri。 What is going wrong here and how can I navigate away from this view?这里出了什么问题,我怎样才能离开这个视图?

Possible solutions I am exploring is creating a custom control extending WebView2 with a bool dependency property mapped to WebView2.Dispose(bool) -> if true, the webview will be disposed.我正在探索的可能解决方案是创建一个扩展 WebView2 的自定义控件,该控件具有映射到 WebView2.Dispose(bool) 的 bool 依赖项属性 -> 如果为真,则将处理 webview。 This however seems like overkill and might bring up other issues relating to ObjectDisposedException 's.然而,这似乎有点矫枉过正,可能会引发与ObjectDisposedException相关的其他问题。

I had the same problem and found it was because I was setting IsAsync=true on the binding.我遇到了同样的问题,发现这是因为我在绑定上设置了 IsAsync=true。

Also inspect the properties you are binding to.还要检查您绑定到的属性。 They always need to be able to get a value that isn't null.他们总是需要能够得到一个不是 null 的值。

When my properties are initialized I set them to new Uri("about:blank").当我的属性被初始化时,我将它们设置为 new Uri("about:blank")。
Additionally, use this._myProp = new UriBuilder(value).Uri??此外,使用 this._myProp = new UriBuilder(value).Uri?? new Uri("about:blank") on the field set to ensure no nulls字段集上的 new Uri("about:blank") 以确保没有空值

Hey Microsoft how about a static Uri.Empty property with about:blank in it?嘿 Microsoft 一个带有 about:blank 的 static Uri.Empty 属性怎么样?

I hope this helps我希望这有帮助

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

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