如何在Metro / WinRT(C#)中显示多个PupupMenu,如下图所示?
这是我的C#代码。
private async void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
PopupMenu menu1 = new PopupMenu();
menu1.Commands.Add(new UICommand("menu1 A", (function) => { }));
menu1.Commands.Add(new UICommand("menu1 B", (function) => { }));
Task<IUICommand> task1 = menu1.ShowAsync(new Point(100, 100)).AsTask<IUICommand>();
PopupMenu menu2 = new PopupMenu();
menu2.Commands.Add(new UICommand("menu2 C", (function) => { }));
menu2.Commands.Add(new UICommand("menu2 D", (function) => { }));
Task<IUICommand> task2 = menu2.ShowAsync(new Point(200, 100)).AsTask<IUICommand>(); // A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Task<IUICommand> task = await Task.WhenAny(task1, task2);
if (task.Result != null)
{
await new MessageDialog(task.Result.Label).ShowAsync();
}
}
答案 0 :(得分:0)
我不知道是否可以显示多个弹出菜单,但是您的代码通过在任务上调用Result
来阻止UI线程。
请改为尝试:
private async void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
PopupMenu menu1 = new PopupMenu();
menu1.Commands.Add(new UICommand("menu1 A", (function) => { }));
menu1.Commands.Add(new UICommand("menu1 B", (function) => { }));
Task<IUICommand> task1 = menu1.ShowAsync(new Point(100, 100)).AsTask<IUICommand>();
PopupMenu menu2 = new PopupMenu();
menu2.Commands.Add(new UICommand("menu2 C", (function) => { }));
menu2.Commands.Add(new UICommand("menu2 D", (function) => { }));
Task<IUICommand> task2 = menu2.ShowAsync(new Point(200, 100)).AsTask<IUICommand>(); // A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Task<IUICommand> task = await Task.WhenAny(task1, task2);
var res = await task;
if (res != null)
{
await new MessageDialog(res.Label).ShowAsync();
}
await Task.WaitAll(task1, task2); // just to await them all
}
答案 1 :(得分:0)
你做不到。 WinRT
不支持显示多个弹出窗口(它会告诉您一个例外)。对于PopupMenu
,MessageBox
以及更多内容都是如此。
你可能会创建一个看起来像双PopupMenu
的自己的窗口,但我会建议不要这样做。