一个超级简单的MVVM光强WP7样? [英] A super-simple MVVM-Light WP7 sample?

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

问题描述

我要寻找演示中最轻的可能的方式下面的例子:

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

这是调用基于SOAP的Web服务模型;定期轮询,以获取最新值(假设SOAP服务返回boolean)。该模型还应该支持调用改变服务器上的布尔一个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.

一个视图模型,使潜在的布尔在视图绑定到控件(例如一个复选框)。

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

与绑定到底层布尔上述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光强的工作,我挣扎,我怀疑,专家可以codea的速度非常快品尝起来像这样...我也怀疑这是一个相当普遍的模式很多应用程序。

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.

推荐答案

米克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光强的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使财产流动两种方式使用双向模式势必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光强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光强的ViewModelBase实现。我真的试图左右逢源,把它工作,但我决定使用ViewModelBase更好,因为它支持广播,我想在我的实际项目,这将是很方便,因为我将有多个的ViewModels喜欢。这似乎有点粗鲁要立足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 =新OnOffSwitchClass();

private OnOffSwitchClass _Switch1 = new OnOffSwitchClass();

public OnOffSwitchClass Switch1 
{
    get
    {
        return _Switch1;
    }
}

和我添加到DispatcherHelper.Initialize()的调用;在我的应用程序()构造函数。

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

这是否正确?

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

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