繁体   English   中英

通过 Process.start 参数传递和接收变量(Winform 到 UWP)

[英]Passing and receiving variables through Process.start parameter (Winform to UWP)

我的 UWP 程序被注册为协议,并通过 Winform 程序的按钮进行调用。 我想通过在 Winform 程序中传递变量值来接收来自 UWP 的值,但我不确定如何在 UWP(XAML) 中接收传递的参数。

窗体

 ProcessStartInfo startinfo = new ProcessStartInfo();

                startinfo.FileName = "safety:";

                startinfo.Arguments = LOGIN.userinfo.user_id;

                Process.Start(startinfo); 

UWP(App.Xaml.cs)

  protected override void OnActivated(IActivatedEventArgs args)

    {
        //Initialize(args);

        if (args.Kind == ActivationKind.Protocol)

        {
           

            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;

            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)

            {
                // Create a Frame to act as the navigation context and navigate to the first page

                rootFrame = new Frame();



                rootFrame.NavigationFailed += OnNavigationFailed;

                

                // Place the frame in the current Window

                Window.Current.Content = rootFrame;                    

            }



            // Always navigate for a protocol launch

            rootFrame.Navigate(typeof(MainPage), eventArgs.Uri.AbsoluteUri);

            // Ensure the current window is active                

            Window.Current.Activate();

        }

    }

只有一次。 当我在 Winform 中单击一个按钮时

您可以使用将要应用的数据添加到Launcher.LaunchUriAsync(Uri, LauncherOptions, ValueSet) 方法中。 然后您可以从OnActivated事件的ProtocolActivatedEventArgs中获取数据。

我已经根据您的代码实现了示例演示,您可以参考以下代码:

UWP 应用程序:

  protected override void OnActivated(IActivatedEventArgs args)
    {
        if (args.Kind == ActivationKind.Protocol)
        {
            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;

            //get the data from Winforms apps
            ValueSet data = eventArgs.Data;
            var d = data["token1"];

            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Window.Current.Content = rootFrame;

            }
            // pass the data to the MainPage
            rootFrame.Navigate(typeof(MainPage), d);            

            Window.Current.Activate();
        }
    }

在 UWP 应用程序的主页中:

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

       var str =  e.Parameter.ToString();

    }

当您调用 LaunchUriAsync 时,将其更改为如下所示:

        // Launch the URI
        var uri = new Uri("alsdk:");
        // create data.
        ValueSet v = new ValueSet();
        v.Add("token1", "12");

        // create LauncherOptions
        var options = new Windows.System.LauncherOptions();
        options.TargetApplicationPackageFamilyName = "PackageFamilyName of Your UWP project";
  
        var success = await Windows.System.Launcher.LaunchUriAsync(uri, options, v);

我用这样的另一种方式解决了这个问题

窗体

 Process.Start("safetytest:"+LOGIN.userinfo.user_id);

UWP(App.Xaml.cs)

protected override void OnActivated(IActivatedEventArgs args)
    {
        ThemeHelper.Initialize();

        Initialize(args);
        if (args.Kind == ActivationKind.Protocol)
        {
            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;

            }
            rootFrame.Navigate(typeof(MainPage), eventArgs.Uri.ToString());//this
            Window.Current.Activate();
        }
    }

UWP 总机

  protected override async void OnNavigatedTo(NavigationEventArgs e)
    {           
        SafetyApp.user_info.user_id = e.Parameter.ToString().Substring(11);  
        var dialog = new MessageDialog(String.Format(user_info.user_id));
        await dialog.ShowAsync();          
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM