michal
michal

Reputation: 163

Cannot Bind Xamarin WebView CanGoBack

Xamain.Forms Binding CanGoBack and CanGoForward throws an error: CanGoBack is not accessible.

It seems to be working on older version of VS.

EDIT: Full code is below.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="WebViewDemo.WebViewDemoPage"> 
<ContentPage.Padding> 
<OnPlatform x:TypeArguments="Thickness" iOS="10, 20, 10, 0" Android="10, 0" WinPhone="10, 0" /> 
</ContentPage.Padding> 
<StackLayout> 
<Entry Keyboard="Url" Placeholder="web address" Completed="OnEntryCompleted" /> <StackLayout Orientation="Horizontal" BindingContext="{x:Reference webView}"> 

<Button Text="&#x21D0;" FontSize="Large" HorizontalOptions="FillAndExpand" IsEnabled="{Binding CanGoBack}" Clicked="OnGoBackClicked" /> 

<Button Text="&#x21D2;" FontSize="Large" HorizontalOptions="FillAndExpand" IsEnabled="{Binding CanGoForward}" Clicked="OnGoForwardClicked" /> 
</StackLayout>

 <WebView x:Name="webView" VerticalOptions="FillAndExpand" Source="https://xamarin.com" /> 
</StackLayout> 
</ContentPage>

Code Behind

public partial class WebViewDemoPage : ContentPage 
{ 
  public WebViewDemoPage() 
  { 
     InitializeComponent();
  }

  void OnEntryCompleted(object sender, EventArgs args) 
  { 
    webView.Source = ((Entry)sender).Text; 
  } 

  void OnGoBackClicked(object sender, EventArgs args)
  { 
    webView.GoBack(); 
  } 

  void OnGoForwardClicked(object sender, EventArgs args) 
  { 
   webView.GoForward(); 
  } 
}

Any ideas how to bind it correctly?

Thank you.

Upvotes: 0

Views: 342

Answers (1)

h0meBrewer
h0meBrewer

Reputation: 31

I know this question is a bit old, but I thought I'd share my solution if anyone (like me) needs to solve this problem.

I solved it by abandoning Binding altogether and using the PropertyChanged event of WebView:

In the Content Page:

<ContentPage>

    <StackLayout Margin="2">
        <StackLayout Orientation="Horizontal">

            <Button x:Name="btnBack" 
                    Text="Back" 
                    HorizontalOptions="Start"  
                    Clicked="OnBackButtonClicked" 
                    IsEnabled="False" />

            <Button x:Name="btnForward" 
                    Text="Fwd" 
                    HorizontalOptions="End"  
                    Margin="5"
                    Clicked="OnForwardButtonClicked" 
                    IsEnabled="False" />

        </StackLayout>

        <Label x:Name="labelLoading" Text="Loading..." IsVisible="false" />
        <WebView x:Name="webView" 
                 WidthRequest="1000" 
                 HeightRequest="1000" 
                 Navigated="WebViewNavigated" 
                 Navigating="WebViewNavigating" 
                 PropertyChanged="WebView_PropertyChanged"/>
    </StackLayout>
</ContentPage>

And in the code behind:

private void WebViewNavigated(object sender, WebNavigatedEventArgs e)
{
    labelLoading.IsVisible = false;
}

private void WebViewNavigating(object sender, WebNavigatingEventArgs e)
{
    labelLoading.IsVisible = true;
}

private void WebView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    switch(e.PropertyName)
    {
        case "CanGoBack":
            btnBack.IsEnabled = ((WebView)sender).CanGoBack;
            break;

        case "CanGoForward":
            btnForward.IsEnabled = ((WebView)sender).CanGoForward;
            break;
    }
}

I hope this helps somebody...

Upvotes: 3

Related Questions