WebView - Is .Source really not bindable?

ClintStLaurentClintStLaurent ✭✭✭✭✭USUniversity ✭✭✭✭✭

Tagline: Am I really this stupid?

I admit I'm not big on web programming. But I had a need for a simple limited browser feature in an app so I tossed in a WebBrowser.

I'm finding that binding the .Source has no effect. But if I change the .Source through a direct setting in code behind the browser navigates to the designated page.

So I go to the documenation:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows
Well... That was less than helpful since it doesn't say boo about such a basic and obvious expectation.

Is this really the way WebView is expected to work: That you can't bind the string property of .Source to a property on a ViewModel?

The default binded property does work at launch

        #region Properties 
        #region BrowserAddy (string)
        private string _BrowserAddy = "https://Google.com";//Reliable default
        public string BrowserAddy
        {
            get
            {
                return _BrowserAddy;
            }

            set
            {
                if (_BrowserAddy == value) return;
                _BrowserAddy = value;
                OnPropertyChanged();
            }
        }
        #endregion BrowserAddy  (string)

        #endregion Properties

But updating it through binding has no affect

            <Button Command="{Binding BrowseToAddressCommand}"
                    CommandParameter="https://RedPillXamarin.com"
                    Text="RPX Binding" />
        private void On_BrowseToAddressCommand(object obj)
        {
            if (obj == null) obj = BrowserAddy;
            //TODO: Validate the new address
            //TODO: Ping the addy to see if we can connect before attempting and causing long hang waiting for timeout

            BrowserAddy = (string)obj;
        }

But a direct change to the UI control does work

           <Button Clicked="btnStarbase3d_Clicked"
                    Text="Starbase3D non-binding" />
        private void btnStarbase3d_Clicked(object sender, EventArgs e)
        {
            Browser.Source = "https://www.etsy.com/shop/starbase3d";
        }

Best Answer

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ US ✭✭✭✭✭
    Accepted Answer

    Solved. WebView works as expected. I had a base class with an event called PropertyChanged that was obfuscating the built in BindableObject.PropertyChanged event - and thus the UI never heard the actual property change.

Answers

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭
    Accepted Answer

    Solved. WebView works as expected. I had a base class with an event called PropertyChanged that was obfuscating the built in BindableObject.PropertyChanged event - and thus the UI never heard the actual property change.

Sign In or Register to comment.