silverlight开发实例(Prism+MVVM+RIA)(二)--创建shell及用户登录
在上篇基本说清了本项目的基本框架,下面开始说下项目的加载和shell。开始之前在建立EF时出现了一个问题,我在数据库中建立了视图,而在EF导入视图时出现因无法匹配主键导致无法导入视图的问题,检查发现是由于视图中sql语句中用了Union,先见Union语句取消再建立EF。
1、首先是建立项目启动的入口程序Bootstrapper,这个类由MefBootstrapper 继承,前面的Blog中已经说明。
2、关于用户登录界面的建立。有2种方式,一种是先建立登录界面,在登录成功后进入Shell。另一种是先建立Shell,然后在Shell启动后加载登录窗口。本例采用的是后者。现在的问题是由于采用MEF+MVVM方式,如何在Shell启动时加载登录窗口,同时在确认登录后进入Shell主窗口。本人采用的是通过在shell初始化时获取登录窗口然后加载,如下:
- protected override void InitializeShell()
- {
- base.InitializeShell();
- App.Current.RootVisual = (UIElement)this.Shell;
- ChildWindow view = ServiceLocator.Current.GetInstance(typeof(LoginForm)) as ChildWindow;
- view.Show();
- }
这样就能够在Shell启动的时候加载登录窗口,见图:
3、登录窗口加载完成,新的问题是如何在Childwindow确认密码或密码错误时,触发窗口关闭或重新输入密码,本例根据网上查到的资料采用的是在Childwindow中添加附加属性的方式,利用附加属性绑定到ViewModel中的一个Bool属性,由此属性的变更引发窗口关闭,另外由于如果密码输入错误需要重新输入而不是关闭窗口,为了简化,在Childwindow中的Closing事件中加入的CodeBehind代码判断是否关闭窗口,不知谁还有好的办法可以交流下。
Childwindow附加属性类:
- public static class ChildwindowDialogResult
- {
- public static readonly DependencyProperty DialogResultProperty =
- DependencyProperty.RegisterAttached("DialogResult", typeof(Boolean?), typeof(ChildwindowDialogResult),
- new PropertyMetadata(OnSetDialogResultCallback));
- public static void SetDialogResult(ChildWindow childWindow, Boolean? dialogResult)
- {
- childWindow.SetValue(DialogResultProperty, dialogResult);
- }
- public static Boolean? GetDialogResult(ChildWindow childWindow)
- {
- return childWindow.GetValue(DialogResultProperty) as Boolean?;
- }
- private static void OnSetDialogResultCallback(DependencyObject dependencyObject,
- DependencyPropertyChangedEventArgs e)
- {
- var childWindow = dependencyObject as ChildWindow;
- if (childWindow != null) childWindow.DialogResult = e.NewValue as bool?;
- }
- }
ChildWindow的Xaml文件
- <controls:ChildWindow x:Class="SLFrameWork.View.LoginForm"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
- xmlns:AttachPropertyInMVVM="clr-namespace:SLFrameWork.Web.Proxy.Common;assembly=SLFrameWork.Web.Proxy"
- Width="400" Height="300"
- AttachPropertyInMVVM:ChildwindowDialogResult.DialogResult="{Binding DialogResult,Mode=TwoWay}"
- Title="用户登录" Closing="ChildWindow_Closing" Style="{StaticResource ChildWindowStyle1}">
- <Grid x:Name="LayoutRoot" Margin="2" >
- <Grid.RowDefinitions>
- <RowDefinition />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <Button x:Name="CancelButton" Content="取消" Command="{Binding Cancle}" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
- <Button x:Name="OKButton" Content="确定" Command="{Binding Confirm}" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
- </Grid>
- </controls:ChildWindow>
ChildWindow的后置代码
- [Export]
- public partial class LoginForm : ChildWindow
- {
- [ImportingConstructor]
- public LoginForm(LoginViewModel vm)
- {
- InitializeComponent();
- this.DataContext = vm;
- }
- private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (this.DialogResult == false)
- {
- e.Cancel = true;
- }
- }
- }
登录窗口的ViewModel
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using SLFrameWork.Web.Proxy.Common;
- using System.ComponentModel.Composition;
- using Microsoft.Practices.Prism.Commands;
- using SLFrameWork.Web.Proxy.Common.Events;
- namespace SLFrameWork.ViewModel
- {
- [Export]
- public class LoginViewModel:MyViewModelBase
- {
- public LoginViewModel()
- {
- }
- private bool? _dialogResult;
- public bool? DialogResult
- {
- get { return _dialogResult; }
- set
- {
- _dialogResult = value;
- RaisePropertyChanged("DialogResult");
- }
- }
- ICommand _confirm;
- public ICommand Confirm
- {
- get
- {
- if (_confirm == null)
- {
- _confirm = new DelegateCommand(OnConfirm);
- }
- return _confirm;
- }
- }
- void OnConfirm()
- {
- MessageBox.Show("登录成功!");
- this.DialogResult = true;
- }
- ICommand _cancle;
- public ICommand Cancle
- {
- get
- {
- if (_cancle == null)
- {
- _cancle = new DelegateCommand(OnCancle);
- }
- return _cancle;
- }
- }
- void OnCancle()
- {
- MessageBox.Show("重新登录!");
- this.DialogResult = false;
- }
- }
- }
本实例尚未加任何逻辑,不过过程基本描述清楚,下面只需要在ViewModel中加入自己的业务逻辑即可实现用户登录了!
最新文章
- Weekly Traning Farm 16
- matlab生成HEX文件-任意信号 大于64K长度
- vmware装redhat该光盘无法被挂载
- sublime text3输入中文的问题.
- Linux sed命令常用方法
- ORACLE STUDY NOTES 02
- python list 中找连续的数字(由网友处学习)
- sql: update from
- 九、 Spring Boot 拦截器
- 新版elasticsearch的插件安装
- 瞎扯设计模式1:单例模式 饿汉模式 懒汉模式 线程安全的单例 singleton 设计模式 java
- JAVA Scanner的简单运用
- 记Git报错-refusing to merge unrelated histories
- PHP实现字符串转义和还原
- Yarn上运行spark-1.6.0
- 每日英语:A Better Way To Treat Anxiety
- python: delete the duplicates in a list
- radio 实现点击两次 第一次点击选中第二次点击取消
- React ref的用法
- 64_g3