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中加入自己的业务逻辑即可实现用户登录了!

待续。。。。。。


 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值