【问题标题】:Catel authentication + MahAppsCatel 认证 + MahApps
【发布时间】:2015-07-08 23:50:33
【问题描述】:

我目前正在使用 MahApps Metro 学习 Catel+Orchestra。 我正在使用 MetroUI 执行来自 Catel.Examples 项目的身份验证示例。 我的问题是当我在 MahAppsService 中创建一个新的 MainWindow 时

        public FrameworkElement GetMainView()
    {
        return new MainWindow();
    }

MainWindowViewModel 的构造函数永远不会被调用

public MainWindowViewModel(UIVisualizerService uiVisualizarService, IAuthenticationProvider authenticationProvider)
    {
        _uiVisualizerService = uiVisualizarService;
        _authenticationProvider = authenticationProvider;
        RoleCollection = new ObservableCollection<string>(new[] { "Read-Only", "Administrator" });
        ShowView = new Command(OnShowViewExecute, OnShowViewCanExecute, "ShowView");
    }

我已将其缩小到构造函数的 2 个依赖项。如果我删除 UIVisualizerService 和 IAuthenticacionProvider 依赖项,构造函数会被正确调用,但 ModelView 稍后需要这两个服务。

我不知道该怎么做才能使它正常工作。

【问题讨论】:

    标签: c# authentication mahapps.metro catel orchestra


    【解决方案1】:

    您必须在 ServiceLocator 中注册 IAuthenticationProvider:

    var serviceLocator = ServiceLocator.Default;
    
    serviceLocator.RegisterType<IAuthenticationProvider, MyAuthenticationProvider>();
    

    请注意,Catel 中的所有服务都会自动为您注册,但您必须自己注册自己的服务(例如,通过使用 ModuleInit 或程序集中的其他入口点)。

    【讨论】:

    • 我已经在应用启动时注册了自定义服务。 public partial class App : Application { protected async override void OnStartup(StartupEventArgs e) { var serviceLocator = ServiceLocator.Default; serviceLocator.RegisterType&lt;IMahAppsService, MahAppsService&gt;(); serviceLocator.RegisterType&lt;IAuthenticationProvider, Authentication.AuthenticationProvider&gt;(); ...
    • OnStartup 的异步无效?我建议不要这样做,因为您无法控制流程(您应该在非异步的事件处理程序上使用 async void)。你能上传一个复制品吗?
    • 你在异步上是对的,我做了正确的改变。
    • 我解决了将视图模型的显式注入添加到主窗口构造函数中的问题。 public MainWindow(MainWindowViewModel _mainwindowviewmodel):base(_mainwindowviewmodel) { InitializeComponent(); } 然后在 MahAppsService 的 GetMainView 中创建视图模型的实例 public FrameworkElement GetMainView() { var mainwindowViewModel = TypeFactory.Default.CreateInstanceWithParametersAndAutoCompletion&lt;MainWindowViewModel&gt;(_uiVisualizerService, _authenticationProvider); return new MainWindow(mainwindowViewModel); } 不确定这是解决问题的正确方法。
    • 已将“已解决”版本上传到 github 中的存储库。随意看看。 github.com/venomAA/CatelAuthenticationExample
    【解决方案2】:

    我通过将视图模型的显式注入添加到主窗口构造函数中解决了这个问题。

    public MainWindow(MainWindowViewModel _mainwindowviewmodel):base(_mainwindowviewmodel)
     {
     InitializeComponent();
     }
    

    向 MahAppsService 类声明 AuthenticationProvider 接口的字段。

    private readonly IAuthenticationProvider _authenticationProvider;
    

    同样将AuthenticationProvider接口的依赖添加到构造函数中。

     public MahAppsService(ICommandManager commandManager, IMessageService messageService, IUIVisualizerService uiVisualizerService, IAuthenticationProvider authenticationProvicer)
        {
            Argument.IsNotNull(() => commandManager);
            Argument.IsNotNull(() => messageService);
            Argument.IsNotNull(() => uiVisualizerService);
            Argument.IsNotNull(() => authenticationProvicer);
            _commandManager = commandManager;
            _messageService = messageService;
            _uiVisualizerService = uiVisualizerService;
            _authenticationProvider = authenticationProvicer;
        }
    

    最后一步是在 MahAppsService 类的 GetMainView 中创建视图模型的实例。

     public FrameworkElement GetMainView()
     {
     var mainwindowViewModel = TypeFactory.Default.CreateInstanceWithParametersAndAutoCompletion<MainWindowView‌​Model>(_uiVisualizerService, _authenticationProvider);
     return new MainWindow(mainwindowViewModel);
     }
    

    请注意,这可能不是最好的方法,但它可以完成工作。如果有人有更好的方法,请随时分享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多