Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Mick N's pointer helped, but what really got me over the hump was this post by Jeremy Likness: <a href="http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html" rel="nofollow noreferrer">http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html</a></p> <p>Here's the sample for the benefit of others (assuming I'm not doing anything really stupid):</p> <p>First, I started using the Mvvm-Light Windows Phone 7 project.</p> <p>I added a checkbox to my MainPage.xaml: <code></p> <pre><code> &lt;CheckBox Content="Switch 1" IsChecked="{Binding Switch1.PowerState, Mode=TwoWay}" Height="72" HorizontalAlignment="Left" Margin="24,233,0,0" Name="checkBox1" VerticalAlignment="Top" Width="428" /&gt; </code></pre> <p></code></p> <p>Notice the IsChecked is bound to Switch1.PowerState using the TwoWay mode so that the property flows both ways.</p> <p>A key learning for me is how to enable communication from my timer callback (TimerCB) which will be running on a new thread to the Silverlight UI thread. I used the Mvvm-Light DispatcherHelper.CheckBeginInvokeOnUI helper which waits on the UI thread.</p> <p>I then had to decide whether to implement INotifyPropertyChanged myself in my model, or use Mvvm-Light's ViewModelBase implementation. I actually tried it both ways and had it working but decided I liked using ViewModelBase better because it supports "broadcast" and I think in my actual project that will be handy because I will have multiple ViewModels. It seems a bit uncouth to be basing a "Model" on ViewModelBase class, but I don't think there's any harm in doing so. (???). </p> <p>My model .cs is below.<br> <code></p> <pre><code>public class OnOffSwitchClass : ViewModelBase // ignore that it's derived from ViewModelBase! { private const Int32 TIMER_INTERVAL = 5000; // 5 seconds private Timer _timer; // Upon creation create a timer that changes the value every 5 seconds public OnOffSwitchClass() { _timer = new System.Threading.Timer(TimerCB, this, TIMER_INTERVAL, TIMER_INTERVAL); } private static void TimerCB(object state) { // Alternate between on and off ((OnOffSwitchClass)state).PowerState = !((OnOffSwitchClass)state).PowerState; } public const string PowerStatePropertyName = "PowerState"; private bool _myProperty = false; public bool PowerState { get { return _myProperty; } set { if (_myProperty == value) { return; } var oldValue = _myProperty; _myProperty = value; // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() =&gt; RaisePropertyChanged(PowerStatePropertyName, oldValue, value, true)); } } } </code></pre> <p></code></p> <p>The MainViewModel.cs was modified to include the following</p> <p><code> private OnOffSwitchClass _Switch1 = new OnOffSwitchClass();</p> <pre><code>public OnOffSwitchClass Switch1 { get { return _Switch1; } } </code></pre> <p></code></p> <p>And I added a call to DispatcherHelper.Initialize(); in my App() constructor.</p> <p>Does this look right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload