How to intercept WebView Navigating event in ViewModel

后端 未结 2 1040
醉话见心
醉话见心 2021-01-22 03:09

My app has a WebView for displaying some contact information. It has a link to a website that I want to load externally using Device.OpenUri(). I\'m using FreshMvvm

2条回答
  •  伪装坚强ぢ
    2021-01-22 03:32

    Having revisited this recently for another project I stumbled across the answer. The updated XAML is:

    
        
            
                
            
        
    
    

    The code in the ViewModel, that matches the tapped url against a list of valid options before opening the link in the device's browser, is:

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

提交回复
热议问题