一个超级简单的 MVVM-Light WP7 示例? [英] A super-simple MVVM-Light WP7 sample?

查看:18
本文介绍了一个超级简单的 MVVM-Light WP7 示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找能够以最轻松的方式展示以下内容的示例:

I am looking for a sample that demonstrates in the lightest way possible the following:

一个调用基于 SOAP 的网络服务的模型;定期轮询以获取最新值(假设 SOAP 服务返回一个布尔值).该模型还应支持调用 SOAP 方法来更改服务器上的布尔值.

A Model that invokes a SOAP based web service; regularly polling to get the latest value (assume the SOAP service returns a boolean). The model should also support invoking a SOAP method that changes the boolean on the server.

一个 ViewModel,它使底层布尔值能够绑定到视图中的控件(例如绑定到复选框).

A ViewModel that enables the underlying boolean to be bound to controls in the View (e.g. to a checkbox).

具有上述复选框控件的视图绑定到底层布尔值.根据轮询间隔,复选框将随着服务器状态的变化而更新.如果单击复选框,事件将被分派到模型,导致服务器更新.

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.

这个示例最适合在 Windows Phone 7 上运行,但在紧要关头,我会对支持 SL3 的东西感到满意(不允许使用 SL4 命令路由).

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).

我正在努力理解如何让 MVVM-Light 为我工作,我怀疑专家可以非常快速地编写这样的示例......我也怀疑这对于很多人来说是一种相当普遍的模式应用.

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.

推荐答案

Mick N 的提示有所帮助,但真正让我克服困难的是 Jeremy Likness 的这篇帖子:http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html

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

这是为了他人的利益而做的示例(假设我没有做任何真正愚蠢的事情):

Here's the sample for the benefit of others (assuming I'm not doing anything really stupid):

首先,我开始使用 Mvvm-Light Windows Phone 7 项目.

First, I started using the Mvvm-Light Windows Phone 7 project.

我在 MainPage.xaml 中添加了一个复选框:<代码>

I added a checkbox to my 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" />

注意 IsChecked 使用 TwoWay 模式绑定到 Switch1.PowerState,以便属性双向流动.

Notice the IsChecked is bound to Switch1.PowerState using the TwoWay mode so that the property flows both ways.

对我来说一个重要的学习是如何启用从我的计时器回调 (TimerCB) 到 Silverlight UI 线程的通信,该回调将在新线程上运行.我使用了 Mvvm-Light DispatcherHelper.CheckBeginInvokeOnUI 助手,它在 UI 线程上等待.

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.

然后我必须决定是在我的模型中自己实现 INotifyPropertyChanged,还是使用 Mvvm-Light 的 ViewModelBase 实现.我实际上尝试了两种方式并让它工作,但我决定我更喜欢使用 ViewModelBase 因为它支持广播",我认为在我的实际项目中这会很方便,因为我将有多个 ViewModel.在 ViewModelBase 类的基础上建立一个模型"似乎有点粗鲁,但我认为这样做没有任何害处.(???).

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. (???).

我的模型 .cs 在下面.
<代码>

My model .cs is below.

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));
        }
    }
}

MainViewModel.cs 已修改为包含以下内容

The MainViewModel.cs was modified to include the following

<代码>私有 OnOffSwitchClass _Switch1 = new OnOffSwitchClass();

private OnOffSwitchClass _Switch1 = new OnOffSwitchClass();

public OnOffSwitchClass Switch1 
{
    get
    {
        return _Switch1;
    }
}

我添加了对 DispatcherHelper.Initialize() 的调用;在我的 App() 构造函数中.

And I added a call to DispatcherHelper.Initialize(); in my App() constructor.

这看起来对吗?

这篇关于一个超级简单的 MVVM-Light WP7 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆