2011-02-04 82 views
0

試圖使用一個登錄窗口,卡利+ MEF登錄窗口與卡利+ MEF MVVM WPF

我提前獲得這兩個警報以及

Warning 1 CA2000 : Microsoft.Reliability : In method 'AppBootstrapper.CreateContainer()', call System.IDisposable.Dispose on object 'container' before all references to it are out of scope. C:\adl\DotNetProjects\CaliburnTest\CaliburnTest\AppBootstrapper.cs 25 CaliburnTest 
Warning 2 CA2000 : Microsoft.Reliability : In method 'AppBootstrapper.CreateContainer()', call System.IDisposable.Dispose on object 'new AggregateCatalog(Enumerable.Select<Assembly, AssemblyCatalog>(this.SelectAssemblies(), AppBootstrapper.CS$<>9__CachedAnonymousMethodDelegate1))' before all references to it are out of scope. C:\adl\DotNetProjects\CaliburnTest\CaliburnTest\AppBootstrapper.cs 25 CaliburnTest 

謝謝,我知道,卡利是要救我很多時候都會喜歡這個工作!

在這個項目中,我的IShell是一個空的接口。

public class AppBootstrapper : Bootstrapper<IShell> 
{ 
    protected override IServiceLocator CreateContainer() 
    { 
     var container = new CompositionContainer(
      new AggregateCatalog(SelectAssemblies().Select(x => new AssemblyCatalog(x))) 
      ); 

     var batch = new CompositionBatch(); 
     //batch.AddExportedValue<IWindowManager>(new WindowManager()); 
     return new MEFAdapter(container); 
    } 
} 



[Export(typeof(IShell))] 
public class MainViewModel : Conductor<IScreen>, IShell 
{ 
    readonly IWindowManager windowManager; 
    [ImportingConstructor] 
    public MainViewModel(IWindowManager windowManager) 
    { 
     this.windowManager = windowManager; 
     var result = windowManager.ShowDialog(new LoginWindowViewModel()); 
     if (result != true) 
     { 
      Application.Current.Shutdown(); 
     } 
    } 
} 

[Export(typeof(LoginWindowViewModel))] 
public class LoginWindowViewModel :Screen 
{ 
    public LoginWindowViewModel() 
    { 
    } 
    public ObservableCollection<User> Users 
    { 
     get 
     { 
      if (_users == null) 
      { 
       _users = new ObservableCollection<User>(){new User("ADMIN","ADMIN","ADMIN")}; 
      } 
      return _users; 
     } 
    } 

    public bool AuthenticateUser(string username, string pass) 
    { 
     Common.Authenticated.CurrentUser = Users.Where<User>(y => y.Login.Trim() == username.Trim()).FirstOrDefault(y => y.Pass.Trim() == pass.Trim()); 
     if (Common.Authenticated.CurrentUser != null) 
      return true; 
     return false; 
    } 
    public bool Authenticated 
    { 
     get 
     { 
      if (Username == null || Username == String.Empty || Password == null || Password == String.Empty) 
       return false; 
      return AuthenticateUser(Username, Password); 
     } 
    } 
    public bool CheckAuthNeeded() {return true;} 
    private ObservableCollection<User> _users; 

    public void Login() 
    { RequestClose(this, new SuccessEventArgs(true));  } 
    public string Username {get;set;} 
    public string Password {get;set;} 

    public event CloseDialogEventHandler RequestClose; 
     public delegate void CloseDialogEventHandler(object sender, SuccessEventArgs e); 
} 

LoginWindowView:

<Grid Height="Auto" Width="Auto"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="2*" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="93*" /> 
     <ColumnDefinition Width="185*" /> 
    </Grid.ColumnDefinitions> 
    <StackPanel Grid.Column="0" Margin="10,10,0,0"> 
     <Label Content="Username:" FontWeight="Bold" VerticalContentAlignment="Center" Margin="0,4,0,0" Padding="5,5,0,5" /> 
     <Label Content="Password:" FontWeight="Bold" VerticalContentAlignment="Center" VerticalAlignment="Stretch" Margin="0,4,0,0" Padding="5,5,0,5" /> 
    </StackPanel> 
    <StackPanel Grid.Column="1" Margin="0,10,10,0"> 
     <dxe:ComboBoxEdit x:Name="User" ItemsSource="{Binding Users}" 
         Padding="0" Margin="5" DisplayMember="Login" ValueMember="Login" Validate="User_Validate" ValidateOnTextInput="True" 
          AutoComplete="True" ImmediatePopup="True" IncrementalFiltering="True" ShowEditorButtons="False" /> 
     <dxe:PasswordBoxEdit x:Name="Pass" Margin="5" Validate="Pass_Validate" ValidateOnTextInput="False" InvalidValueBehavior="AllowLeaveEditor" /> 
    </StackPanel> 
    <Button Content="Login" Grid.Row="1" Height="23" HorizontalAlignment="Center" 
      x:Name="Login" VerticalAlignment="Top" Width="75" IsDefault="True" Grid.ColumnSpan="2" /> 
</Grid> 

LoginWindowView代碼隱藏:

public partial class LoginWindowView 
{ 
    public LoginWindowView() 
    { 
     InitializeComponent(); 
    } 
    private void User_Validate(object sender, ValidationEventArgs e) 
    { 

     if (e.Value == null) 
     { 
      e.IsValid = false; 
      return; 
     } 
     var _vm = GetDataContext(); 
     var u = _vm.Users.FirstOrDefault<User>(x => x.Login.Trim() == ((string)e.Value).Trim().ToUpper()); 

     if (u == null) 
     { 
      e.SetError("Invalid Login Name", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Information); 
      _vm.Username = string.Empty; 
      e.IsValid = false; 
     } 
     else 
      _vm.Username = User.Text; 
    } 
    public LoginWindowViewModel GetDataContext() 
    { 
     return (LoginWindowViewModel)DataContext; 
    } 
    private void Pass_Validate(object sender, ValidationEventArgs e) 
    { 
     if (e.Value == null) 
     { 
      e.IsValid = false; 
      return; 
     } 
     var _vm = GetDataContext(); 
     _vm.Password = ((string)e.Value).ToUpper(); 
     if (_vm.Authenticated == false) 
     { 
      e.SetError("Wrong Password.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical); 
      e.IsValid = false; 
     } 
    } 
    private void Window_Closed(object sender, EventArgs ee) 
    { 
     GetDataContext().RequestClose -= (s, e) => 
     { 
      this.DialogResult = e.Success; 
     }; 
    } 
    private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     if (e.OldValue != null) 
     { 
      ((LoginWindowViewModel)e.OldValue).RequestClose -= (s, ee) => 
      { 
       this.DialogResult = ee.Success; 
      }; 
     } 
     ((LoginWindowViewModel)e.NewValue).RequestClose += (s, ee) => 
     { 
      this.DialogResult = ee.Success; 
     }; 
    } 
} 

public class User 
{ 
    public string FullName { get; private set; } 
    public string Login { get; private set; } 
    public string Pass { get; private set; } 

    public User(string fullName, string login, string pass) { FullName = fullName; Login = login; Pass = pass; } 
    public static User CreateUser(string fullName, string login,string pass) 
    { 
     return new User(fullName, login,pass); 
    } 
} 




    public static class Authenticated 
    { 
     public static string AuthString { get; set; } 
     public static User CurrentUser { get; set; } 
     public static string UserId 
     { 
      get 
      { 
       if (CurrentUser != null) 
        return CurrentUser.Login; 
       return string.Empty; 
      } 
     } 
    } 

的MainView:卡利的

<Window x:Class="CaliburnTest.Views.MainView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainView" Height="300" Width="300"> 
    <Grid> 
     <Label>Test</Label> 
    </Grid> 
</Window> 
+0

你能清理這個問題嗎?這會讓答案更容易 – devdigital 2011-02-05 09:53:15

回答

0

其實羅布·艾森伯格是非常有益的,他幫了我這個問題。

問題是,當我切換到校準時,LoginView是第一個要打開的窗口,它在MainView窗口打開之前關閉。

窗口將第一個窗口作爲主窗口打開。當主窗口關閉時,窗口檢查是否有其他窗口打開,否則關閉應用程序。

他提供了一個可能的解決方案,讓loginviewmodel成爲shell並在打開mainviewmodel後關閉它。