2012-08-09 86 views
2

在我的Caliburn Bootstrapper中,我试图验证用户是否有权运行我的应用程序。如果他们不这样做,我需要给他们留言并退出。以下代码在MessageBox.Show()调用中抛出NullReferenceException。即使我在ValidateUserHasPermissionsToRun()Configure()方法之前调用ComposeMef(),它仍然有错误。在Caliburn Bootstrapper中显示消息框Configure()

public class MyBootstrapper : Bootstrapper<DropWindowViewModel> 
{ 

    // irrelevant methods omitted for brevity 

    protected override void Configure() 
    { 
     this.InitializeSecurity(); 
     this.ValidateUserHasPermissionToRun(); 
     this.ComposeMef(); 
    } 

    private void ComposeMef() 
    { 
     AggregateCatalog catalog = new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()); 

     this.container = new CompositionContainer(catalog); 

     CompositionBatch batch = new CompositionBatch(); 

     batch.AddExportedValue<IWindowManager>(new WindowManager()); 
     batch.AddExportedValue<IEventAggregator>(new EventAggregator()); 
     batch.AddExportedValue(this.container); 
     batch.AddExportedValue(catalog); 

     this.container.Compose(batch); 
    }  

    private void ValidateUserHasPermissionToRun() 
    { 
     User user = SecurityContext.Current.SecurityUser; 

     if (!user.HasPrivilege(Constants.PrivilegeLoadData)) 
     { 
     // throws an exception 
     MessageBox.Show("You do not have access to VIPER. Please contact the help desk if you need help."); 
     this.TerminateApplication(); 
     } 
    } 
} 

什么是正确的方法来处理?对于这样简单的事情,我只想显示一个消息框。我真的不想编写一个全新的ViewModel/View。如果我必须使用不同的ViewModel,我将如何切换哪个ViewModel Caliburn使用?或者我应该在DropWindowViewModel上设置一个属性来触发不同的界面?

回答

3

我建议这是应该在你的根视图模型内完成的事情。引导程序应该就是这样。它加载您的应用程序所需的资源&配置它们。之后,它完成了。检查用户是否可以运行该应用程序是应用程序逻辑的一部分因此&应该放在根视图模型中。

+0

我可以买那个。好答案。 – michaelkoss 2012-08-22 13:32:52