刚学MVVM 百度了很多概念性的东西 也参考了网上的例子 基本有了了解
但是我发现 我做了一个登录页面以后 我跳转咋办呢? VM里面咋做跳转? 问了一下其他的群友得到了一些启发。感谢“上海*松”
我仅在此作为标记,贴出发给我的demo。
一下是登录页面:VLoginWindow.xaml 很简单 上面就一个按钮:
<Window x:Class="ShenJingBingDemo.View.VLoginWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:vm="clr-namespace:ShenJingBingDemo.ViewModel" Title="登录" Height="300" Width="300"> <Window.DataContext> <vm:VMLoginWindow /> </Window.DataContext> <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <ei:CallMethodAction TargetObject="{Binding}" MethodName="MainWindow_Loaded"/> </i:EventTrigger> </i:Interaction.Triggers> <Grid> <Button Content="登录" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100" Height="60"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <ei:CallMethodAction TargetObject="{Binding}" MethodName="Btn_Login_Click"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> </Grid> </Window>
接下来是VM: VMLoginWindow.cs 跳转到MainWindow.xaml 主面板
using System.Windows; namespace ShenJingBingDemo.ViewModel { public class VMLoginWindow : NotificationObject { #region 变量 Window win; #endregion 变量 #region 事件 public void MainWindow_Loaded(object sender, RoutedEventArgs e) { win = sender as Window; // e.Handled = true; } public void Btn_Login_Click(object sender, RoutedEventArgs e) { MainWindow mainwin = new MainWindow(); win.Close(); mainwin.ShowDialog(); // e.Handled = true; } #endregion 事件 } }
还有demo里实现响应的NotificationObject.cs
using System; using System.ComponentModel; using System.Linq.Expressions; using System.Reflection; namespace ShenJingBingDemo { public abstract class NotificationObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } protected void RaisePropertyChanged(params string[] propertyNames) { if (propertyNames == null) throw new ArgumentNullException("propertyNames"); foreach (var name in propertyNames) { this.RaisePropertyChanged(name); } } protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression) { var propertyName = ExtractPropertyName(propertyExpression); this.RaisePropertyChanged(propertyName); } public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression) { if (propertyExpression == null) { throw new ArgumentNullException("propertyExpression"); } var memberExpression = propertyExpression.Body as MemberExpression; if (memberExpression == null) { throw new ArgumentException("PropertySupport_NotMemberAccessExpression_Exception", "propertyExpression"); } var property = memberExpression.Member as PropertyInfo; if (property == null) { throw new ArgumentException("PropertySupport_ExpressionNotProperty_Exception", "propertyExpression"); } var getMethod = property.GetGetMethod(true); if (getMethod.IsStatic) { throw new ArgumentException("PropertySupport_StaticExpression_Exception", "propertyExpression"); } return memberExpression.Member.Name; } } }
代码是这么个代码,上面加的俩命名空间其实是引用
C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries
下面的俩类库Microsoft.Expression.Interactions 和System.Windows.Interactivity。看样子是blend的里面的,用的时候记得添加引用。
标记一下,匆匆下班,再见各位。