通过 Process.start 参数传递和接收变量(Winform 到 UWP)
Passing and receiving variables through Process.start parameter (Winform to UWP)
我的UWP程序注册为协议,通过Winform程序的按钮调用。
我想通过在 Winform 程序中传递变量值来接收来自 UWP 的值,但我不确定如何在 UWP 中接收传递的参数(XAML).
winform
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();
}
}
Only once. when i click a button in Winform
您可以使用将要应用的数据添加到 Launcher.LaunchUriAsync(Uri, LauncherOptions, ValueSet) Method。然后你可以从 OnActivated
事件的 ProtocolActivatedEventArgs
中获取数据。
我已经根据你的代码实现了一个示例demo,你可以参考下面的代码:
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);
我用另一种方式解决了这个问题
winform
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();
}
我的UWP程序注册为协议,通过Winform程序的按钮调用。 我想通过在 Winform 程序中传递变量值来接收来自 UWP 的值,但我不确定如何在 UWP 中接收传递的参数(XAML).
winform
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();
}
}
Only once. when i click a button in Winform
您可以使用将要应用的数据添加到 Launcher.LaunchUriAsync(Uri, LauncherOptions, ValueSet) Method。然后你可以从 OnActivated
事件的 ProtocolActivatedEventArgs
中获取数据。
我已经根据你的代码实现了一个示例demo,你可以参考下面的代码:
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);
我用另一种方式解决了这个问题
winform
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();
}