Is it safe to use MvxNotifyPropertyChanged as a replacement for implementing INotifyPropertyChanged?

StackOverflow https://stackoverflow.com/questions/20490576

質問

In order to get PropertyChanged to fire in NUnit tests, I had to set ShouldAlwaysRaiseInpcOnUserInterfaceThread(false). Are there any repercussions to this when I later use the class as a ViewModel? Maybe I should be setting up a user interface thread in NUnit? Help!

public interface ISomething : INotifyPropertyChanged
{
}

public class Something : MvxNotifyPropertyChanged, ISomething
{
   public Something()
   {
      ShouldAlwaysRaiseInpcOnUserInterfaceThread(false);
   }

   private int _num;
   public int Num
   {
      get { return _num; }
      set { if (_num != value) { _num = value; RaisePropertyChanged(() => Num); }
   }
}
役に立ちましたか?

解決

By default MvvmCross marshals calls like RaisePropertyChanged onto the UI thread for the convenience of developers.

If you want to disable this on an individual object, you can call ShouldAlwaysRaiseInpcOnUserInterfaceThread(false); for that object (this is a method call rather than a property as properties on ViewModel objects are generally reserved for INotifyPropertyChanged use)

If you want to disable this by default on all objects then you can use Mvx.Resolve<IMvxSettings>().AlwaysRaiseInpcOnUserInterfaceThread = false;

If during testing you want to provide a mock implementation for the UI thread marshalling, then see for example the N=29 video in http://mvvmcross.blogspot.co.uk/ - with some MockDispatcher code inside https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-29-TipCalcTest/TipCalcTest.Tests

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top