Search code examples
maui.net-mauimaui-windows

How to use WebView.Reload() within SizeChanged event in .NET MAUI?


I need to refresh webView control when the ui size changed. I used SizeChanged event in some controls like Border, WebView, etc SizeChanged event. But I got this system.invalidoperationexception 'a method was called at an unexpected time error. I got this error at the beginning of running the application. The way I used:

<WebView x:Name="webView" SizeChanged="OnSizeChanged" HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand">
     <WebView.Source>
          ...
     </WebView.Source>
</WebView> 
private void OnSizeChanged(object sender, EventArgs e)
{
      webView.Reload();
}

Probably I used it in wrong way, or could it be a bug?


Solution

  • I solved the problem when I used Reload method in try-catch:

    private void OnSizeChanged(object sender, EventArgs e)
    {
        try
        {
            webView.Reload();
        }
        catch(Exception ex)
        {
            
        } 
    }