2015-12-08 19 views
2

關閉ContentDialog上的「Enter」我一直在試圖讓一個簡單的ContentDialogTextBox關閉當用戶點擊TextBox輸入同時還。可悲的是,即使ContentDialog響應Esc,即使沒有TextBox也不能正常工作。如何UWP

我希望有一種方法可以從KeyDown處理程序TextBox的處理程序中設置結果,但似乎ContentDialog缺少這個?!

回答

5

您可以關閉使用Hide()方法ContentDialog在TextBoxKeyDown處理程序,簡單的例子:

ContentDialog c = new ContentDialog(); 

var tb = new TextBox(); 

tb.KeyDown += (sender, args) => 
{ 
    if (args.Key == VirtualKey.Enter) 
    { 
      c.Hide(); 
    } 
}; 

c.Content = tb; 
c.ShowAsync(); 

編輯: 但它似乎更復雜,當你想關閉該對話框而不TextBox。您需要申請開通全球Window.Current.CoreWindow.KeyDown事件:

ContentDialog c = new ContentDialog(); 

Window.Current.CoreWindow.KeyDown += (sender, args) => 
{ 
     if (args.VirtualKey == VirtualKey.Enter) 
     { 
      c.Hide(); 
     } 
}; 
c.ShowAsync(); 
+1

謝謝,這確實有效。我不知道爲什麼'Hide'不需要一個參數來設置'ShowAsync'的'ContentDialogResult'與'ContentDialogResult.None'不同的東西 –

+0

c.Hide()可能會設置c之前的一個可能的改進。 tag = ContentDialogResult.Primary。對於VirtualKey.Escape,請設置c.tag = ContentDialogResult.Secondary。如果c.ShowAsync的結果是ContentDialogResult.None,那麼可以將結果推斷爲對話框的標籤,將其轉換爲ContentDialogResult。 – zax

2

這是我會得到我的一個ContentDialogResult.Primary輸入

我將此添加到我的ContentDialog最終的解決方案:

public new IAsyncOperation<ContentDialogResult> ShowAsync() 
    { 
     var tcs = new TaskCompletionSource<ContentDialogResult>(); 

     CaptionTB.KeyDown += (sender, args) => 
     { 
      if (args.Key != VirtualKey.Enter) return; 
      tcs.TrySetResult(ContentDialogResult.Primary); 
      Hide(); 
      args.Handled=true; 
     }; 

     var asyncOperation = base.ShowAsync(); 
     asyncOperation.AsTask().ContinueWith(task => tcs.TrySetResult(task.Result)); 
     return tcs.Task.AsAsyncOperation(); 
    } 

不幸ShowAsync是不是虛擬的,所以我不得不new的功能。它對我來說效果不錯!

+1

真棒,你是一個天才! – Tsayper

+0

CaptionTB不被VS識別,並沒有建議使用...如何解決? – kamp

+0

這是** T ** ext ** B **牛我用於**標題**。您將在XAML中將其定義爲'' –

0

簡短的回答是,它沒有(乾淨)沒有解決方法和黑客,將保留識別哪個按鈕被按下的功能。長的答覆是,這是幸運的很乾淨,容易繼承ContentDialog做正是我們想要的:

using System; 
using System.Threading.Tasks; 
using Windows.Foundation; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Input; 

namespace NeoSmart.Dialogs 
{ 
    class HotkeyContentDialog : ContentDialog 
    { 
     public new event TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> PrimaryButtonClick; 
     public new event TypedEventHandler<ContentDialog, ContentDialogButtonClickEventArgs> SecondaryButtonClick; 

     public ContentDialogResult Result { get; set; } 
     public new async Task<ContentDialogResult> ShowAsync() 
     { 
      var baseResult = await base.ShowAsync(); 
      if (baseResult == ContentDialogResult.None) 
      { 
       return Result; 
      } 
      return baseResult; 
     } 

     protected override void OnKeyUp(KeyRoutedEventArgs e) 
     { 
      if (e.Key == Windows.System.VirtualKey.Enter) 
      { 
       Result = ContentDialogResult.Primary; 
       PrimaryButtonClick?.Invoke(this, default(ContentDialogButtonClickEventArgs)); 
       Hide(); 
      } 
      else if (e.Key == Windows.System.VirtualKey.Escape) 
      { 
       Result = ContentDialogResult.Secondary; 
       SecondaryButtonClick?.Invoke(this, default(ContentDialogButtonClickEventArgs)); 
       Hide(); 
      } 
      else 
      { 
       base.OnKeyUp(e); 
      } 
     } 
    } 
} 

只需使用HotkeyContentDialog代替ContentDialog和一切都會好的。