Click here to Skip to main content
15,916,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a WPF desktop app. I am using webview2 inside a WPF Page which is loaded in the MainWindow.xaml

When trying to set the source of the webview2 from the code behind it does not load the HTML file

When setting the source from the xaml to https://www.microsoft.com it loads without any issues.

I have set the Build Action for the HTML files as Content > Copy if Newer

Here is the code

<Grid Grid.Row="1">
    <TabControl x:Name="tcPolicies" Height="675">
        <TabItem x:Name="tabTerms" Header="Terms & Conditions" Background="#677682" FontSize="18" Foreground="#FF7553">
            <wv2:WebView2 x:Name="wv2Terms"/>
        </TabItem>
        <TabItem x:Name="tabPrivacy" Header="Privacy" Background="#677682" FontSize="18" Foreground="#FF7553">
            <Grid>
                <wv2:WebView2 x:Name="wv2Privacy" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Grid>
        </TabItem>
        <TabItem x:Name="tabDataPrivacy" Header="Data Privacy" Background="#677682" FontSize="18" Foreground="#FF7553">
            <Grid>
                <wv2:WebView2 x:Name="wv2DataPrivacy" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></wv2:WebView2>
            </Grid>
        </TabItem>
        <TabItem x:Name="tabDataLoss" Header="Data Loss Policy" Background="#677682" FontSize="18" Foreground="#FF7553">
            <Grid>
                <wv2:WebView2 x:Name="wv2DataLoss" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Grid>
        </TabItem>
    </TabControl>
</Grid>


private void Page_Loaded(object sender, RoutedEventArgs e)
       {
           Stash_About stash_About=new Stash_About();
           stash_About.LoadPolicies();
       }

       private void LoadPolicies()
       {
           try
           {
               string filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppData\\Policies", "Terms.html");
               if(File.Exists(filename))
               {
                   wv2Terms.Source = new Uri(filename, UriKind.Absolute);
               }
           }
           catch (Exception ex)
           {

           }
       }


What I have tried:

Set the source from xaml file. Used the NavigateToString & Navigate methods. Nothing works
Posted
Updated 8-Jan-23 6:24am

1 solution

To navigate to a URL, it's going to be something like this:
myWebViewInstanceName.CoreWebView2.Navigate(urlString);
 
Share this answer
 
Comments
Christopher Fernandes 8-Jan-23 13:03pm    
It throws NullReference exception
Dave Kreskowiak 8-Jan-23 13:26pm    
I take it you're navigating directly from the code when your app starts? You have to wait for CoreWebView2 to initialize before you tell it to do something. Add this code:
async void InitializeAsync()
{
   await webView.EnsureCoreWebView2Async(null);
}

Then call InitializeAsync() from your window constuctor method:
public MainWindow()
{
   InitializeComponent();

   InitializeAsync();
}


In case you missed it, this is the documentation for WebVeiw2: Introduction to Microsoft Edge WebView2 - Microsoft Edge Development | Microsoft Learn[^]
Richard Deeming 9-Jan-23 6:39am    
NB: async void will crash the entire process if an exception is thrown. For a fire-and-forget scenario, the recommendation is to use a Task-returning method, and assign the returned task to a discard to avoid the compiler warning:
async Task InitializeAsync() { ... }
public MainWindow()
{
    InitializeComponent();
    _ = InitializeAsync();
}

If you have other code that depends on this task completing first, then store the task in a field and await it before executing the dependent code.
Dave Kreskowiak 9-Jan-23 8:01am    
Yeah, I wasn't going for production ready code. The snippets I posted are directly from MS example code, 'cause I'm heavily medicated and very tired. :)
Christopher Fernandes 9-Jan-23 8:44am    
Get Well Soon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900