Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Correct way for updating data bound property from non-UI thread.

$
0
0
  1. What is the correct way for updating data bound property from non-UI thread?
  2. Is it necessary to wrap code in a call to InvokeOnMainThread as it is described here: https://developer.xamarin.com/guides/ios/user_interface/controls/part_2_-_working_with_the_ui_thread/?
    Or is it enough to simply update property from non-UI thread as it is shown in ClockViewModel example here: https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_bindings_to_mvvm/?

Example of code:

class MyModel : INotifyPropertyChanged
{
    public string myProperty;

    public event PropertyChangedEventHandler PropertyChanged;

    public MyModel()
    {
        myProperty = null;

        new System.Threading.Thread(new System.Threading.ThreadStart(() => {
            // perform some operation that requires time.
            // …
            // update data bound property.
            MyProperty = “Sense of life has been found. It is to: …”;
       })).Start();
    }

    public string MyProperty
    {
        set
        {
            if (myProperty != value)
            {
                myProperty = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs("MyProperty"));
                }
            }
        }
        get
        {
            return myProperty;
        }
    }
}

Thank you.


Viewing all articles
Browse latest Browse all 77050

Trending Articles