如何将 StartupEventArgs 传递给 Prism 应用程序中的其他 ViewModel

How to pass StartupEventArgs to other ViewModels in Prism application

我们正在使用Prism 7。是否有任何最佳实践来传递获得的StartupEventArg参数 从 App.xaml.cs 的 Prism OnStartup 方法到其他 ViewModel。事件聚合器在此方法中不可用,因此看起来我们无法使用这种将数据传递到视图模型的方法。

谢谢

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        if (e.Args.Length > 0)
        {
            UriBuilder builder = new UriBuilder(e.Args[0]);
            var result = HttpUtility.ParseQueryString(builder.Query);
            var username = result["username"];
            var password = result["password"];
           // how to get these to viewmodels
        }
        
    }

how to get these to viewmodels?

您创建了一个向任何感兴趣的人提供原始参数的服务,很可能是另一个将它们解析为用户名和密码的服务。

示例:

internal class EnvironmentCommandLineArgumentsProvider : ICommandLineArgumentsProvider
{
    #region ICommandLineArgumentsProvider
    public IReadOnlyList<string> Arguments => _arguments.Value;
    #endregion

    #region private
    private readonly Lazy<IReadOnlyList<string>> _arguments = new Lazy<IReadOnlyList<string>>( () => Environment.GetCommandLineArgs() );
    #endregion
}

internal class CommandLineInitialCredentialsProvider : IInitialCredentialsProvider
{
    public CommandLineInitialCredentialsProvider( ICommandLineArgumentsProvider commandLineArgumentsProvider )
    {
        _credentials = new Lazy<(string UserName, string Password)>( () =>
        {
            if (commandLineArgumentsProvider.Arguments.Count > 0)
            {
                var builder = new UriBuilder(commandLineArgumentsProvider.Arguments[0]);
                var result = HttpUtility.ParseQueryString(builder.Query);
                return (result["username"], result["password"]);
            }
            return (null, null);
        });
    }

    #region IInitialCredentialsProvider
    public string UserName => _credentials.Value.UserName;
    public string Password => _credentials.Value.Password;
    #endregion

    #region private
    private readonly Lazy<(string UserName, string Password)> _credentials;
    #endregion
}