xamlxamarinxamarin.formsfreshmvvm

XAML WebView binding to HTML source not working


I have a XAML page layout defined that includes a WebView populated from an HTML string:

<WebView HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
    <WebView.Source>
        <HtmlWebViewSource Html="{Binding Flag.Description}" />
<!--    <HtmlWebViewSource Html="&lt;html>&lt;body>&lt;p>The HTML string.&lt;/p>&lt;/body>&lt;/html>" />-->
    </WebView.Source>
</WebView>

When I hard code the string into the XAML it displays correctly, but it will not bind to the Flag.Description string. The XAML includes a couple of labels that bind correctly but I can't find any reason why the WebView source isn't binding correctly.


Solution

  • I wasn't able to get the binding to work in XAML but there is a code-based solution to the problem. I'm using FreshMvvm so in the page model (not the code-behind for the XAML page) I created a new property that references the HTML string:

    public HtmlWebViewSource WebViewSource
    {
        get { 
            return new HtmlWebViewSource { Html = Flag.Description }; 
        }
    }
    

    And the WebView element in the XAML page becomes:

    <WebView
        HorizontalOptions="Fill"
        VerticalOptions="FillAndExpand"
        Source="{Binding WebViewSource}" />