tig*_*tig 11 c# silverlight windows-phone-7 mvvm-light
我正在寻找一个样本,以最轻微的方式演示以下内容:
一个调用基于SOAP的Web服务的模型; 定期轮询以获取最新值(假设SOAP服务返回一个布尔值).该模型还应支持调用更改服务器上的布尔值的SOAP方法.
一个ViewModel,它允许底层布尔值绑定到View中的控件(例如,复选框).
具有上述复选框控件的视图绑定到基础布尔值.根据轮询间隔,复选框将随服务器状态的变化而更新.如果单击该复选框,则会将事件分派给模型,从而导致服务器更新.
最好这个样本可以在Windows Phone 7上运行,但是在紧要关头我会对支持SL3的东西感到满意(不允许使用SL4命令路由).
我正在努力学习如何让MVVM-Light为我工作,我怀疑专家可以很快地对这样的代码进行编码...我也怀疑这是很多应用程序的相当常见的模式.
米克N为指针帮助,但真正让我渡过了难关是这个职位由Jeremy Likness: http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html
下面是其他人的利益的样品(假设我没有做什么傻事):
首先,我开始使用MVVM光强的Windows Phone 7项目.
我在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" />
Run Code Online (Sandbox Code Playgroud)
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
private OnOffSwitchClass _Switch1 = new OnOffSwitchClass();
public OnOffSwitchClass Switch1
{
get
{
return _Switch1;
}
}
Run Code Online (Sandbox Code Playgroud)
<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" />
Run Code Online (Sandbox Code Playgroud)
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了对DispatcherHelper.Initialize()的调用; 在我的App()构造函数中.
这看起来不错吗?
| 归档时间: |
|
| 查看次数: |
8430 次 |
| 最近记录: |