How to get Navigating event of WebView in ViewModel in Xamarin.Forms ??

neeraj_goelneeraj_goel USMember ✭✭

i want to get Navigating event of WebView in ViewModel in Xamarin.Forms. Please tell me how i can achieve it ??

Best Answer

  • neeraj_goelneeraj_goel US ✭✭
    Accepted Answer

    Below code helps me to achieve it :

    **public class CustomWebView : WebView
    {
        public static readonly BindableProperty NavigatingCommandProperty =
            BindableProperty.Create(nameof(NavigatingCommand), typeof(ICommand), typeof(CustomeWebView), null);
        public ICommand NavigatingCommand
        {
            get { return (ICommand)GetValue(NavigatingCommandProperty); }
            set { SetValue(NavigatingCommandProperty, value); }
        }
    
        public CustomWebView()
        {
            Navigating += (s, e) =>
            {
                WebViewClass _WebViewClass = new WebViewClass();
                _WebViewClass.sender = s;
                _WebViewClass.e = e;
                if (NavigatingCommand?.CanExecute(e) ?? false)
                    NavigatingCommand.Execute(_WebViewClass);
            };
        }
    }
    
    public class WebViewClass
    {
        public object sender { get; set; }
        public WebNavigatingEventArgs e { get; set; }
    
    }**
    

    I have created a CustomWebView class and in that class constructor i bind a navigating command.
    Hope same will help of other as it does not require any other plugin.

    Thanks

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited April 30

    Try to use Corcav.Behaviors plugin. The plugin can easily help you binding the Navigating Event to ViewModel.
    page.xaml

    <WebView HeightRequest="800" 
                         WidthRequest="800"
                         Source="https://www.google.com"
                         >
        <behaviors:Interaction.Behaviors>
            <behaviors:BehaviorCollection>
                <behaviors:EventToCommand 
                    EventName="Naviating"
                    Command="{Binding navigatingCommand}"
                    PassEventArgument="True"
                    />
            </behaviors:BehaviorCollection>
        </behaviors:Interaction.Behaviors>
    </WebView>
    

    ViewModel.cs

    public Command<WebNavigatingEventArgs> NavigatingCommand
    {
        get
        {
            return navigatingCommand ?? (navigatingCommand = new Command<WebNavigatingEventArgs>(
                (param) =>
                {
                    if (param != null && -1 < Array.IndexOf(_uris, param.Url))
                    {
                        Device.OpenUri(new Uri(param.Url));
                        param.Cancel = true;
                    }
                },(param) => true));
        }
    }
    

    Check the link: https://stackoverflow.com/questions/36560415/how-to-intercept-webview-navigating-event-in-viewmodel
    Sample here: https://github.com/corradocavalli/Corcav.Behaviors

  • XamtasticXamtastic Member ✭✭✭

    I think you're looking for the "Navigated" and that should be MVVM safe

  • neeraj_goelneeraj_goel USMember ✭✭
    Accepted Answer

    Below code helps me to achieve it :

    **public class CustomWebView : WebView
    {
        public static readonly BindableProperty NavigatingCommandProperty =
            BindableProperty.Create(nameof(NavigatingCommand), typeof(ICommand), typeof(CustomeWebView), null);
        public ICommand NavigatingCommand
        {
            get { return (ICommand)GetValue(NavigatingCommandProperty); }
            set { SetValue(NavigatingCommandProperty, value); }
        }
    
        public CustomWebView()
        {
            Navigating += (s, e) =>
            {
                WebViewClass _WebViewClass = new WebViewClass();
                _WebViewClass.sender = s;
                _WebViewClass.e = e;
                if (NavigatingCommand?.CanExecute(e) ?? false)
                    NavigatingCommand.Execute(_WebViewClass);
            };
        }
    }
    
    public class WebViewClass
    {
        public object sender { get; set; }
        public WebNavigatingEventArgs e { get; set; }
    
    }**
    

    I have created a CustomWebView class and in that class constructor i bind a navigating command.
    Hope same will help of other as it does not require any other plugin.

    Thanks

Sign In or Register to comment.