wp8使用mvvm模式简单例子(二)---登陆功能,事件触发
2014-07-09 15:21
519 查看
首先,还是需要一个Model类来为UI层的元素提供数据源
和一个继承ICommand接口的类LoginCommand
然后为前台的UI元素绑定数据源:
这样看起来似乎没有任何问题
启动调试,点击按钮进行判断,完美运行
但是肯定是有地方不对劲
那到底是哪里的问题?
仔细想想mvvm的原则
在xaml.cs后台不直接操作UI元素
Model提供数据源
UI元素绑定数据源
LoginCommand的Execute方法进行业务逻辑处理
其实在前面的Execute方法的业务处理代码中已经违反了mvvm的规定
不能有控制UI的部分
上面这么写就相当于asp.net的三层中在Bll层突然来了个Response.Redirect一样
这很明显是不允许的!
那么要如何修改呢?
首先
既然对MessageBox明确了不能出现在这里
那么只能在xaml.cs后台文件中进行处理了
那么后台又不能包含有任务业务处理的代码
他怎么才能知道什么时候要Show一下信息,然后Show什么信息呢?
这就要用到一个事件机制了
先定义一个委托
然后在LoginModel中定义一个事件
在后台页面加载的事件中对该事件添加方法
而这时,LoginCommand的Execute方法就需要进行修改了
既然在页面加载的时候 就已经为LoginModel的LoginCompelete事件添加完了显示MessageBox的方法
那么在Execute中需要做的事情就只是 判断用户的账号密码
然后根据结果触发这个事件就ok了
但是因为事件本质是一个私有的委托+add和remove两个属性
所以该事件只能在内部被触发
所以在LoginModel中需要提供一个方法,让外界能够通过该方法触发LoginCompelete事件
之后就可以再Execute方法中方便的触发事件了
修改完成!
public class LoginModel:DependencyObject { public string Uid { get { return (string)GetValue(UidProperty); } set { SetValue(UidProperty, value); } } // Using a DependencyProperty as the backing store for Uid. This enables animation, styling, binding, etc... public static readonly DependencyProperty UidProperty = DependencyProperty.Register("Uid", typeof(string), typeof(LoginModel), null); public string Pwd { get { return (string)GetValue(PwdProperty); } set { SetValue(PwdProperty, value); } } // Using a DependencyProperty as the backing store for Pwd. This enables animation, styling, binding, etc... public static readonly DependencyProperty PwdProperty = DependencyProperty.Register("Pwd", typeof(string), typeof(LoginModel),null ); public ICommand LoginCmd { get { return new LoginCommand(); } } }
和一个继承ICommand接口的类LoginCommand
<pre name="code" class="csharp">public class LoginCommand:ICommand { public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { LoginModel login = (LoginModel) parameter; if (login.Uid == "admin" && login.Pwd == "111") { MessageBox.Show("ok"); } else { MessageBox.Show("error"); } } public event EventHandler CanExecuteChanged; }
然后为前台的UI元素绑定数据源:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="14,10,10,-10" Grid.ColumnSpan="2" DataContext="{StaticResource loginModel}"> <TextBox Name="txtUid" Text="{Binding Uid,Mode=TwoWay}" HorizontalAlignment="Left" Height="72" Margin="115,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="224"/> <TextBox Name="txtPwd" Text="{Binding Pwd,Mode=TwoWay}" HorizontalAlignment="Left" Height="72" Margin="115,138,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="224"/> <TextBlock Text="账号:" HorizontalAlignment="Left" Margin="28,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-0.146,-0.259"/> <TextBlock Text="密码:" HorizontalAlignment="Left" Margin="28,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-0.146,-0.259"/> <Button Content="登陆" Command="{Binding LoginCmd}" CommandParameter="{Binding}" HorizontalAlignment="Left" Margin="28,246,0,0" VerticalAlignment="Top"/> </Grid>
这样看起来似乎没有任何问题
启动调试,点击按钮进行判断,完美运行
但是肯定是有地方不对劲
那到底是哪里的问题?
仔细想想mvvm的原则
在xaml.cs后台不直接操作UI元素
Model提供数据源
UI元素绑定数据源
LoginCommand的Execute方法进行业务逻辑处理
其实在前面的Execute方法的业务处理代码中已经违反了mvvm的规定
public void Execute(object parameter) { LoginModel login = (LoginModel) parameter; if (login.Uid == "admin" && login.Pwd == "111") { MessageBox.Show("ok"); } else { MessageBox.Show("error"); } }这里只能写逻辑处理的代码
不能有控制UI的部分
上面这么写就相当于asp.net的三层中在Bll层突然来了个Response.Redirect一样
这很明显是不允许的!
那么要如何修改呢?
首先
既然对MessageBox明确了不能出现在这里
那么只能在xaml.cs后台文件中进行处理了
那么后台又不能包含有任务业务处理的代码
他怎么才能知道什么时候要Show一下信息,然后Show什么信息呢?
这就要用到一个事件机制了
先定义一个委托
public delegate void LoginResult(bool success);
然后在LoginModel中定义一个事件
public event LoginResult LoginCompelete;
在后台页面加载的事件中对该事件添加方法
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { LoginModel model = (LoginModel)this.Resources["loginModel"]; model.LoginCompelete += new LoginResult(model_LoginCompelete); } void model_LoginCompelete(bool success) { if (success) { MessageBox.Show("ok!"); } else { MessageBox.Show("error!"); } }
而这时,LoginCommand的Execute方法就需要进行修改了
既然在页面加载的时候 就已经为LoginModel的LoginCompelete事件添加完了显示MessageBox的方法
那么在Execute中需要做的事情就只是 判断用户的账号密码
然后根据结果触发这个事件就ok了
但是因为事件本质是一个私有的委托+add和remove两个属性
所以该事件只能在内部被触发
所以在LoginModel中需要提供一个方法,让外界能够通过该方法触发LoginCompelete事件
public void FirLoginCompelete(bool success) { if (LoginCompelete != null) { LoginCompelete(success); } }
之后就可以再Execute方法中方便的触发事件了
public void Execute(object parameter) { LoginModel login = (LoginModel) parameter; if (login.Uid == "admin" && login.Pwd == "111") { login.FirLoginCompelete(true); } else { login.FirLoginCompelete(false); } }
修改完成!
相关文章推荐
- wp8使用mvvm模式简单例子(二)---登陆功能,事件触发
- wp8使用mvvm模式简单例子
- wp8使用mvvm模式简单例子
- 【WPF开发备忘】使用MVVM模式开发中列表控件内的按钮事件无法触发解决方法
- 我谈Silverlight架构和模式运用1-- ModelView模式最简单教程-讲解无代码触发事件,以及类型转换器的强大功能
- c#委托和事件的简单使用,简单介绍observer设计模式
- Silverlight中使用MVVM(7):DataGrid中触发Button的Click事件
- 【转】ASP.NET MVC框架下使用MVVM模式-KnockOutJS+JQ模板例子
- ASP.NET使用Cookie简单实现记住登陆状态功能
- ext4.2入门简单小例子(button的事件--对话框的几种使用情况)
- WPF中使用MVVM模式进行简单的数据绑定
- c#学习笔记之九 事件(event)的使用,一个简单的例子揭示其设计理念
- MVVM-Light模式,在dataGrid的模板下,绑定事件不触发的原因已经服务端排序的实现
- 在C#中使用代理的方式触发事件 的简单习作
- MVVM开发模式MVVM Light Toolkit中使用事件和参数传递
- 使用struts2框架搭建简单的登陆功能
- MVVM-Light模式,在dataGrid的模板下,绑定事件不触发的原因已经服务端排序的实现
- MVVM开发模式MVVM Light Toolkit中使用事件和参数传递
- MVVM-Light模式,在dataGrid的模板下,绑定事件不触发的原因已经服务端排序的实现
- Android MVC,MVP,MVVM模式入门——重构登陆注册功能