简体   繁体   English

一个超级简单的MVVM-Light WP7样本?

[英]A super-simple MVVM-Light WP7 sample?

I am looking for a sample that demonstrates in the lightest way possible the following: 我正在寻找一个样本,以最轻微的方式演示以下内容:

A Model that invokes a SOAP based web service; 一个调用基于SOAP的Web服务的模型; regularly polling to get the latest value (assume the SOAP service returns a boolean). 定期轮询以获取最新值(假设SOAP服务返回一个布尔值)。 The model should also support invoking a SOAP method that changes the boolean on the server. 该模型还应支持调用更改服务器上的布尔值的SOAP方法。

A ViewModel that enables the underlying boolean to be bound to controls in the View (eg to a checkbox). 一个ViewModel,它允许底层布尔值绑定到View中的控件(例如,复选框)。

A View with the above checkbox control bound to the underlying boolean. 具有上述复选框控件的视图绑定到基础布尔值。 Depending on the poll interval the checkbox will update as the server's state changes. 根据轮询间隔,复选框将随服务器状态的变化而更新。 If the checkbox is clicked the event will be dispatched to the model causing the server to be updated. 如果单击该复选框,则会将事件分派给模型,从而导致服务器更新。

Optimally this sample will work on Windows Phone 7, but in a pinch I'd be happy with something that supported SL3 (no use of SL4 command routing allowed). 最好这个样本可以在Windows Phone 7上运行,但是在紧要关头我会对支持SL3的东西感到满意(不允许使用SL4命令路由)。

I am struggling with trying to understand how to make MVVM-Light work for me and I suspect that an expert could code a sample up like this very quickly... I also suspect this is a fairly common pattern for a lot of apps. 我正在努力学习如何让MVVM-Light为我工作,我怀疑专家可以很快地对这样的代码进行编码...我也怀疑这是很多应用程序的相当常见的模式。

Mick N's pointer helped, but what really got me over the hump was this post by Jeremy Likness: http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html Mick N的指针有所帮助,但真正让我超越驼峰的是Jeremy Likness的这篇文章: http ://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html

Here's the sample for the benefit of others (assuming I'm not doing anything really stupid): 这是为了他人利益的样本(假设我没做任何真正愚蠢的事情):

First, I started using the Mvvm-Light Windows Phone 7 project. 首先,我开始使用Mvvm-Light Windows Phone 7项目。

I added a checkbox to my MainPage.xaml: 我在MainPage.xaml中添加了一个复选框:

  <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" /> 

Notice the IsChecked is bound to Switch1.PowerState using the TwoWay mode so that the property flows both ways. 请注意,IsChecked使用TwoWay模式绑定到Switch1.PowerState,以便属性双向流动。

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. 对我来说,一个关键的学习是如何启用从我的计时器回调(TimerCB)进行通信,该回调将在新线程上运行到Silverlight UI线程。 I used the Mvvm-Light DispatcherHelper.CheckBeginInvokeOnUI helper which waits on the UI thread. 我使用了Mvvm-Light DispatcherHelper.CheckBeginInvokeOnUI帮助程序,它在UI线程上等待。

I then had to decide whether to implement INotifyPropertyChanged myself in my model, or use Mvvm-Light's ViewModelBase implementation. 然后,我必须决定是否在我的模型中自己实现INotifyPropertyChanged,或者使用Mvvm-Light的ViewModelBase实现。 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. 我实际上尝试了两种方式,并让它工作,但我决定更好地使用ViewModelBase,因为它支持“广播”,我认为在我的实际项目中,这将是方便的,因为我将有多个ViewModel。 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. 在ViewModelBase类上建立一个“模型”似乎有点粗鲁,但我不认为这样做有任何伤害。 (???). (???)。

My model .cs is below. 我的模特.cs在下面。

 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(() => RaisePropertyChanged(PowerStatePropertyName, oldValue, value, true)); } } } 

The MainViewModel.cs was modified to include the following MainViewModel.cs已修改为包含以下内容

private OnOffSwitchClass _Switch1 = new OnOffSwitchClass();

 public OnOffSwitchClass Switch1 { get { return _Switch1; } } 

And I added a call to DispatcherHelper.Initialize(); 我添加了对DispatcherHelper.Initialize()的调用; in my App() constructor. 在我的App()构造函数中。

Does this look right? 这看起来不错吗?

Check this blog post out by Joost van Schaik that was recently linked (by kP from memory?) over at wp7 forums. 查看最近由wp7论坛链接的Joost van Schaik发布的这篇博文(来自内存中的kP?)。

http://dotnetbyexample.blogspot.com/2010/07/using-mvvm-light-to-drive-windows-phone.html http://dotnetbyexample.blogspot.com/2010/07/using-mvvm-light-to-drive-windows-phone.html

I doubt you'll find a "sample" that also happens to implement your "requirements", but at least with a sample that does what your question title describes you can learn and then apply your more detailed requirements to it. 我怀疑你会找到一个“样本”,它也恰好实现了你的“要求”,但至少有一个样本可以做你的问题标题所描述的,你可以学习,然后将更详细的要求应用到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM