winui-3 c# I am new to WinUi3 but I worked on WinForms app before. Webview2 have to be installed using NuGet in WinForms app, but it is included in WinUi3. I found that WebView2 in WinUi3 works Normally in window(MainWindow) but If I add WebView2 to a page like:
<?xml version="1.0" encoding="utf-8"?>
<Page
<!--Some properties-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="9*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.ColumnSpan="2" Background="Blue"/>
<controls:WebView2 x:Name="Browser" Grid.Row="1" Grid.ColumnSpan="2" Source="https://www.google.com" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" DefaultBackgroundColor="Black"/>
</Grid>
</Page>
And Navigate the page in main window with Frame:
//Adding a Tab with Frame
muxc.TabViewItem NewTab = new();
Frame TabFrame = new();
NewTab.Content = TabFrame;
NewTab.Header = "New";
TabFrame.Navigate(typeof(TabPage));
TabControl.TabItems.Add(NewTab);
the window will look like this. It shows the blue rectangle, which means the page loaded correctly. The part under the blue rectangle is WebView2, which does’t even show DefaultBackgroundColor
.
I used C# code await Browser.EnsureCoreWebView2Async();
and Console.WriteLine(Browser.Parent.ToString());
to see if my webview2 exist. The first code just keep waiting and the second code throws NullReferenceException. Please tell me how to fix it.
Eric Nien is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.