Agat
Agat

Reputation: 4779

MvvmCross custom binding via INotifyPropertyChanged

I've got similar situation to this, but in Touch. Trying to deal with that via INotifyPropertyChanged though.

My code is the following:

set.Bind(txtSearch).For(x => x.Text).To(x => x.SearchText);

where txtSearch is my custom wrapper of UISearchBar. So, I can not inherit from MvxNotifyPropertyChanged as I already inherit from UIView (the wrapper is view).

Text property is:

 public string Text { get
    {
        return _search.Text;
    } set
    {
        _search.Text = value;
        RaisePropertyChanged(() => Text);
    }
}

and I fire it on the SearchBar text changing (which works).

I've also added the following:

    public event PropertyChangedEventHandler PropertyChanged;

    protected IMvxMainThreadDispatcher Dispatcher
    {
        get { return MvxMainThreadDispatcher.Instance; }
    }

    protected void InvokeOnMainThread(Action action)
    {
        if (Dispatcher != null)
            Dispatcher.RequestMainThreadAction(action);
    }
    protected void RaisePropertyChanged<T>(Expression<Func<T>> property)
    {
        var name = this.GetPropertyNameFromExpression(property);
        RaisePropertyChanged(name);
    }

    protected void RaisePropertyChanged(string whichProperty)
    {
        var changedArgs = new PropertyChangedEventArgs(whichProperty);
        RaisePropertyChanged(changedArgs);
    }

    protected void RaisePropertyChanged(PropertyChangedEventArgs changedArgs)
    {
        // check for subscription before going multithreaded
        if (PropertyChanged == null)
            return;

        InvokeOnMainThread(
            () =>
            {
                var handler = PropertyChanged;

                if (handler != null)
                    handler(this, changedArgs);
            });
    }

But when everything gets to RaisePropertyChanged, then I see that PropertyChanged is empty (so, seems no code is subscribed for my object). Which, of course, makes no notifications further.

I have similar situation but with some object inherited directly from MvxNotifyPropertyChanged, which seems working fine. Does that mean, that MvvmCross only can deal with such objects but not ones which generally use INotifyPropertyChanged?

Thank you!

Upvotes: 4

Views: 4412

Answers (1)

Stuart
Stuart

Reputation: 66882

INotifyPropertyChanged is used on the ViewModel side for property changes.

On the View side, MvvmCross uses DependencyProperty bindings on Windows, and C# methods, properties and events on the Xamarin platforms.

INotifyPropertyChanged isn't provided by default on the View side - since no off-the-shelf View objects support INotifyPropertyChanged, then there was no point in trying to bind to it within any of the MvvmCross View platforms.

However, the binding system is extensible - so if anyone wants to write INotifyPropertyChanged based views and wants to include a custom INotifyPropertyChanged binding for the View side, then they can do that following the steps similar to In MvvmCross how do I do custom bind properties and following examples linked from https://speakerdeck.com/cirrious/custom-bindings-in-mvvmcross

If they want to write an INotifyPropertyChanged-based system for the View side, then I'm sure this could be achieved using a custom binding approach - but it's not something I've personally done. I would expect such a custom binding to work both for INotifyPropertyChanged and for MvxNotifyPropertyChanged too (since MvxNotifyPropertyChanged implements INotifyPropertyChanged) - but I guess it would be up to the author to decide on the mechanics of that.

Upvotes: 3

Related Questions