【问题标题】:Wpf.Webview2.CoreWebview2.get returned null. Object reference not set to instance of an objectWpf.Webview2.CoreWebview2.get 返回 null。对象引用未设置为对象的实例
【发布时间】:2021-10-15 23:29:57
【问题描述】:

我正在尝试创建一个添加了 html 文件的示例桌面 wpf 应用程序。我正在使用 webview2。

这是我的标记:

<Wpf:WebView2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="webContainer" />

和我的代码:

public MainWindow()
{
    InitializeComponent();

    string url1 = Directory.GetCurrentDirectory() + "\\site\\index1.html";
    webContainer.CoreWebView2.Navigate(url1);
}

我在Navigate() 行收到以下错误

用户未处理的异常
System.NullReferenceException :对象引用未设置为对象的实例。

 `Wpf.Webview2.CoreWebview2.get` returned null.

谁能帮我找出错误?

【问题讨论】:

标签: c# .net wpf visual-studio webview


【解决方案1】:

我找不到纠正 Navigate() 用法的解决方案,但切换到 Source() 解决了问题。这是我的解决方案

我还将名称更改为 x:Name

 public MainWindow()
  {    
 InitializeComponent();

Uri url1 = new Uri(Directory.GetCurrentDirectory() + "\\site\\index1.html");
webContainer.Source = url1;
 }

希望对遇到同样问题的人有所帮助

【讨论】:

  • 嗨,@beginner854。这个解决方案能解决您的问题吗?如果解决了,您可以单击“✔”接受它作为答案。有助于社区成员解决类似问题。
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多