Binding HTML string to Webview

Ronald Rex 141 Reputation points

Hi Friends. Using the XAML below I can show a webview with hard coded HTML. But what if I wanted to bind the HTML to the webview so that I could dynamically update the webview. I have tried following numerous discussions involving the webview to figure out how to make it work, but no success. I am trying to get this to work in a .Net Maui application Any help would be greatly appreciated.

 <WebView HeightRequest="100" WidthRequest="200">  
                                <H1>.NET MAUI</H1>  
                                <P>Welcome to WebView.</P>  
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
1,055 questions
No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 13,501 Reputation points Microsoft Vendor


    You could binding HTML string to Webview via Source property.

    Please refer to the following code sample:

       // in your ViewModel  
       public HtmlWebViewSource HtmlWebViewSource {  
       get { return htmlWebViewSource; }  
       set { SetProperty(ref htmlWebViewSource, value); } }  
       private HtmlWebViewSource htmlWebViewSource;  
       // Set Html content in ViewModel  
       HtmlWebViewSource = new HtmlWebViewSource  
             Html = @"<HTML><BODY><H1>.NET MAUI</H1><P>Welcome to WebView.</P></BODY></HTML>"  
       // use the binding HTML content  
       <WebView HeightRequest="100" WidthRequest="200" Source="{Binding HtmlWebViewSource}">  

    Best Regards,

    Alec Liu.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Oldest