續上篇。
在上篇基本說清了本項目的基本框架,下面開始說下項目的加載和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中加入自己的業務邏輯即可實現用戶登錄了!
待續。。。。。。