Issue
I'm new at Xamarin.Forms, so excuse me if I don't use the correct words. I created a solution called "WebBrowser" in order to create a simple mobile browser; at the moment I'm working on the common files MainPage.xaml and MainPage.xaml.cs, and I'm testing the code on an Android 5.1 smartphone. I use Microsoft Visual Studio 2017 15.5.4.
In MainPage.xaml I specified the following code
<Button Text="Back"
x:Name="backButton"
IsEnabled="False"
Clicked="previousPage"/>
<Button Text="Next"
x:Name="nextButton"
IsEnabled="False"
Clicked="nextPage"/>
<WebView x:Name="appWebView"
Source="https://www.google.com/"
Navigating="onLoadingPage"
Navigated="onPageLoaded"/>
so when a page is loaded, the method onPageLoaded()
is called.
This method contains the following code
private void onPageLoaded(object sender, EventArgs e)
{
if (appWebView.CanGoBack)
backButton.IsEnabled = true;
else
backButton.IsEnabled = false;
if (appWebView.CanGoForward)
nextButton.IsEnabled = true;
else
nextButton.IsEnabled = false;
}
in order to enable/disable the "arrow" buttons when the user can/can't go to the previous/next page.
In this situation appWebView.CanGoBack
and appWebView.CanGoForward
always return false, even when it should be true. But if I keep the buttons enabled and I check their values when the user clicks one of the buttons, both return the correct value.
Why doesn't it work inside onPageLoaded()
method?
Solution
It's not the exact answer to what you asked for, but you can use the Bindings resource to do such a thing. Like this:
<Button Text="Back"
x:Name="backButton"
IsEnabled="{Binding CanGoBack, Source={Reference appWebView}}"
Clicked="previousPage"/>
<Button Text="Next"
x:Name="nextButton"
IsEnabled="{Binding CanGoForward, Source={Reference appWebView}}"
Clicked="nextPage"/>
<WebView x:Name="appWebView"
Source="https://www.google.com/"
Navigating="onLoadingPage"/>
It's simpler. Take a look at this article if you wanna know more about bindings on Xamarin.Forms.
Thus you can get rid of the onPageLoaded
event handler if it exists only for this.
I hope it helps.
[EDIT] Spelling Correction CanGoForward
Answered By - Diego Rafael Souza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.