2014-05-22 6 views
0

Comment afficher plusieurs PupupMenu ressemble à l'image ci-dessous dans Metro/WinRT (C#)?Comment afficher plusieurs PopupMenu dans Metro WinRT?

Image Multiple PopupMenu

Voici mon code 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

Depuis que je suis certes trop paresseux pour copier/coller ceci et essayer moi-même: Où votre code échoue? Si je devais deviner, il est probablement impossible d'avoir plusieurs PopupMenus ouverts à la fois - cela pourrait casser LightDismiss. Dans ce cas, la solution consisterait à combiner tous les éléments de menu dans un menu contextuel ou dans un contrôle personnalisé. –

+0

a échoué à: Tâche task2 = menu2.ShowAsync (nouveau point (200, 100)) AsTask (); –

+0

ooops. Désolé mon mauvais. Je n'ai pas fait défiler vers la droite ... –

Répondre

0

Vous ne pouvez pas. WinRT ne supporte pas l'affichage de plusieurs popups (il vous dit avec une exception). C'est vrai pour PopupMenu, MessageBox et plus.

Vous pourriez probablement créer votre propre fenêtre qui ressemble à un double PopupMenu mais je déconseille.

+0

Je vais essayer. Deux ListView, ou quelque chose, merci. –

+0

[note de correction] System.InvalidOperationException –

0

Je ne sais pas si l'affichage de plus d'un menu contextuel est possible, mais votre code bloque le thread d'interface utilisateur en appelant Result la tâche.

Essayez ceci:

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 
} 
+0

Il ne bloque pas. WhenAny renvoie la première tâche terminée, ce qui signifie que task.Result retournera immédiatement. – i3arnon

+1

Mon mauvais, @ I3arnon. Mais encore une pratique dangereuse. –

Questions connexes