WebView2 control doesn't working....

c00012 746 Reputation points
2021-08-25T11:37:09.977+00:00

Hello,

I got a following view:
126318-ice-screenshot-20210825-202913.png

in above view, when I click "Help" button, I want to show my blog in another window.
so, I added window as follows:
<Window x:Class="PayEstPractice.Views.HelpWeb"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PayEstPractice.Views"
xmlns:local1="clr-namespace:PayEstPractice.ViewModel"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
Background="AliceBlue"
ResizeMode="NoResize"
mc:Ignorable="d"
Title="HelpWeb" Height="500" Width="750">
<Window.DataContext>
<local1:PersonalViewModel/>
</Window.DataContext>
<Grid>
<DockPanel>
<wv2:WebView2 Name="webView" Source="https://mamdero.blogspot.com" />
</DockPanel>
</Grid>
</Window>
also, I wrote a command as follows:

    public ICommand HelpNaviCommand { get; }//to navigate help view  

    HelpNaviCommand = new RelayCommand(HelpNavi);  

    private void HelpNavi()  
    {  
        Views.HelpWeb hw = new Views.HelpWeb();  
        string text = "https://mamdero.blogspot.com";  

        hw.webView.CoreWebView2.Navigate(text);  
    }  

after build and running above code,

126391-ice-screenshot-20210825-203539.png

if someone pick my error, I'd be very appreciated.

thanks,

c00012

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. c00012 746 Reputation points
    2021-08-26T02:34:46+00:00

    I fixed the problem by changing the code as follows:

        private async void HelpNavi()
        {
            Views.HelpWeb hw = new Views.HelpWeb();
            hw.Show();
            await hw.webView.EnsureCoreWebView2Async(null);
            hw.webView.CoreWebView2.Navigate(WebViewSource);
        }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,571 Reputation points
    2021-08-25T18:30:23.22+00:00

    WebView2 Constructor says:

    Upon creation, the control's CoreWebView2 property will be null. .... There are two ways to cause the CoreWebView2 to be created: 1) Call the EnsureCoreWebView2Async(CoreWebView2Environment) method. .... 2) Set the Source property.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.