WebView.Navigated not triggered

gitChef30gitChef30 ✭✭Member ✭✭

Hi,
could I please have an explanation for why if I declare an instance of WebView, without declaring it in the View, it doesn't trigger the Navigated event?

    public void NavigateTo(string url)
    {
        WebView web = new WebView();
        web.Source = url;
        web.Navigated += Web_Navigated;
    }

while this

    <WebView x:Name="webView" IsVisible="False"/>`

    webView.Source = url;
    webView.Navigated += Web_Navigated;

works without any problem. Thanks in advance!

Tagged:

Posts

  • JohnHardmanJohnHardman mod GBUniversity mod

    @gitChef30 - If I am understanding your question correctly, I don't think you are comparing like with like. Just to make sure:

    (a) In the first case, are you saying that you are instantiating a WebView but not including that instance in the UI hierarchy of a pushed page?

    (b) In the second case, are you calling InitialiseComponent, or otherwise including the XAML, in the UI hierarchy of a pushed page?

    Until a WebView is rendered (that includes when IsVisible = false) as part of the UI, navigation events in the WebView's rendered form (and hence in the C# object) will not occur. If the WebView has not been rendered, it is just a POCO (plain old C# object), that will not be doing anything other than taking up some memory.

  • gitChef30gitChef30 ✭✭ Member ✭✭
    edited June 15

    @JohnHardman
    Thanks for the reply John!

    Yeah in the first case I'm not including it in the UI. I've done it previously in a pure UWP project and it worked, so I thought the logic would have been the same.

    In the second case I'm declaring a WebView control in the UI (XAML)
    <WebView x:Name="webView" IsVisible="False"/>

    and using it in this way it works:

        webView.Source = url;
        webView.Navigated += Web_Navigated;
    

    I thought the first method was going to work as in the UWP app

  • JohnHardmanJohnHardman mod GBUniversity mod

    @gitChef30 said:
    I thought the first method was going to work as in the UWP app

    The difference would have been that in the UWP app, the object that you created was the type that is used natively in UWP. Whereas, when using XF, the WebView is not a native object, it is just a XF representation. The native object does not exist until rendering occurs. Only at that point will the events start firing.

  • gitChef30gitChef30 ✭✭ Member ✭✭

    @JohnHardman
    Thank you so much, now I understand!

  • JohnHardmanJohnHardman mod GBUniversity mod

    @gitChef30 said:
    @JohnHardman
    Thank you so much, now I understand!

    Please remember to mark as Accepted Answer if question answered :-)

Sign In or Register to comment.