我的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();
}
}
只有一次。当我点击Winform
中的按钮时
你可以使用添加你想要应用到启动器的数据。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);
我用另一种方法解决了这个问题
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();
}