Xamarin Forms WebView Check When Website Address Changed

被刻印的时光 ゝ 提交于 2020-01-02 06:34:14

问题


I have the following code that sets up a WebView inside my Xamarin.Forms Cross Platform application:

ReportsListWebView = new WebView()
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    BackgroundColor = Xamarin.Forms.Color.Transparent
};            

URLReportsListWebView = new  UrlWebViewSource
{
    Url = "http://192.168.0.96/MyWebsite/App/MiniMyWebsite?ActionType=Listing&Params=Reports"
};

ReportsListWebView.Source = URLReportsListWebView;

grid.Children.Add(ReportsListWebView, 0, 4, 0, 1);

The situation is that there is listing within the website that I am referencing in the WebView. When the user selects an item in the listing on the webpage it has javascript that changes the url of the website (appends #SelectedItem=1 to the url). I just want to be able to recognize this change from within the application.

I've checked the URLReportsListWebView.Url but it doesn't seem to update with the latest changes. Any ideas on how to achieve this?

Thanks


回答1:


This ended up being a limitation on the xamarin forms webview control. The work around was to create a custom renderer which the Xamarin support provided me a great same showing how to accomplish this at github.com/jgold6/XFormsWebViewCustomRenderer/tree/master




回答2:


When I've done a few tests against http://www.yahoo.com it appears to be updating the WebView.Source property ok, even with query string attribues.

Are you just updating the location of the current webpage rather than navigating to a new page?

Maybe this could be the reason why its not working for you?

If so, after the change, you will then be able to monitor the .Source property for the newly navigated webpage as there is no event handler or anything to hook into to get notified when a page has been navigated to / fully loaded.

Update 1:-

Try the following that is working for me.

It should produce updates similar to the following:-

http://www.yahoo.com

https://fr.yahoo.com/?p=us

https://fr.news.yahoo.com/syrie-jihadistes-exécutent-160-soldats-43-casques-bleus-050134941.html

http://www.tv3g.bouquettv.mobi/wap/landing/landing2.asp?c=LFYAH_TVGREEN_AAAMMM&IDLanding=16972&tag=0&Alea=7.906741E-02&Al=MM201408290946299694

Code:-

        StackLayout objStackLayout = new StackLayout()
        {
        };
        //
        WebView objWebView1 = new WebView();            
        objWebView1.HeightRequest = 300;
        objStackLayout.Children.Add(objWebView1);
        //
        UrlWebViewSource objUrlToNavigateTo = new UrlWebViewSource()
        {
            Url = "http://www.yahoo.com"
        };
        objWebView1.Source = objUrlToNavigateTo;
        //
        //
        Button cmdButton1 = new Button();
        cmdButton1.Text = "Show Me Current Url";
        objStackLayout.Children.Add(cmdButton1);
        //
        cmdButton1.Clicked += ((o2, e2) =>
            {
                System.Diagnostics.Debug.WriteLine((objWebView1.Source as UrlWebViewSource).Url);
            });
        //
        //
        this.Content = objStackLayout;



回答3:


If you don't yet have a custom renderer, you'll need to refer to Xamarin documentation to learn how to custom render Xamarin.Forms WebView.

If you already have the custom renderer, inside the CustomRenderer object, you should access the NativeWebview object and assign HandleShouldStartLoad to its ShouldStartLoad event handler. My mistake was that I assigned HandleShouldStartLoad to the event handler of the renderer itself, which won't work.



来源:https://stackoverflow.com/questions/25517298/xamarin-forms-webview-check-when-website-address-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!