C# 如何关闭“上的ContentDialog”;输入“;超宽带
我一直在尝试获得一个简单的C# 如何关闭“上的ContentDialog”;输入“;超宽带,c#,uwp,C#,Uwp,我一直在尝试获得一个简单的ContentDialog,当用户在文本框中按下Enter键时,会关闭文本框。遗憾的是,即使ContentDialog响应Esc,如果没有文本框,它也无法工作 我希望有一种方法可以从文本框的KeyDown处理程序内部设置结果,但是ContentDialog似乎缺少这个 您可以使用TextBoxKeyDown处理程序中的Hide() ContentDialog c = new ContentDialog(); var tb = new TextBox(); tb.Ke
ContentDialog
,当用户在文本框中按下Enter键时,会关闭文本框
。遗憾的是,即使ContentDialog响应Esc,如果没有文本框
,它也无法工作
我希望有一种方法可以从文本框
的KeyDown
处理程序内部设置结果,但是ContentDialog
似乎缺少这个 您可以使用TextBox
KeyDown
处理程序中的Hide()
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();
这是我的最终解决方案,它将在回车时为我生成一个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();
}
public新IAsyncOperation ShowAsync()
{
var tcs=new TaskCompletionSource();
CaptionTB.KeyDown+=(发送方,参数)=>
{
if(args.Key!=VirtualKey.Enter)返回;
tcs.TrySetResult(ContentDialogResult.Primary);
隐藏();
args.Handled=true;
};
var asyncOperation=base.ShowAsync();
asyncOperation.AsTask().ContinueWith(task=>tcs.TrySetResult(task.Result));
返回tcs.Task.asasAsyncOperation();
}
不幸的是,showascync
不是虚拟的,所以我不得不new
这个函数。不过对我来说效果不错 简单的回答是,如果没有保留识别按下哪个按钮的功能的变通方法和技巧,就不可能(干净地)做到这一点。很长一段时间的答案是,幸运的是,很容易将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);
}
}
}
}
使用系统;
使用System.Threading.Tasks;
使用Windows基金会;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Input;
名称空间NeoSmart.Dialogs
{
类HotkeyContentDialog:ContentDialog
{
public new event TypedEventHandler primary按钮单击;
public new event TypedEventHandler secondary按钮单击;
public ContentDialogResult{get;set;}
公共新异步任务ShowAsync()
{
var baseResult=await base.ShowAsync();
if(baseResult==ContentDialogResult.None)
{
返回结果;
}
返回baseResult;
}
受保护的覆盖无效OnKeyUp(KeyRoutedEventArgs e)
{
if(e.Key==Windows.System.VirtualKey.Enter)
{
结果=ContentDialogResult.Primary;
PrimaryButtonClick?.Invoke(这是默认值(ContentDialogButtonClickEventArgs));
隐藏();
}
else if(e.Key==Windows.System.VirtualKey.Escape)
{
结果=ContentDialogResult.Secondary;
SecondaryButtonClick?.Invoke(这是默认值(ContentDialogButtonClickEventArgs));
隐藏();
}
其他的
{
基元键合(e);
}
}
}
}
只要使用HotkeyContentDialog
而不是ContentDialog
,一切都会好起来。Mahmouds解决方案非常完美!应被接受为答案。以下是我进一步修改的HotkeyContentDialog类:
public 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();
return baseResult == ContentDialogResult.None ? Result : baseResult;
}
protected override void OnKeyUp(KeyRoutedEventArgs e)
{
switch (e.Key)
{
case Windows.System.VirtualKey.Enter:
Result = ContentDialogResult.Primary;
PrimaryButtonClick?.Invoke(this, default);
Hide();
break;
case Windows.System.VirtualKey.Escape:
Result = ContentDialogResult.Secondary;
SecondaryButtonClick?.Invoke(this, default);
Hide();
break;
default:
base.OnKeyUp(e);
break;
}
}
}
公共类HotkeyContentDialog:ContentDialog
{
public new event TypedEventHandler primary按钮单击;
public new event TypedEventHandler secondary按钮单击;
public ContentDialogResult{get;set;}
公共新异步任务ShowAsync()
{
var baseResult=await base.ShowAsync();
返回baseResult==ContentDialogResult.None?结果:baseResult;
}
受保护的覆盖无效OnKeyUp(KeyRoutedEventArgs e)
{
开关(电子钥匙)
{
案例Windows.System.VirtualKey.Enter:
结果=ContentDialogResult.Primary;
PrimaryButtonClick?.Invoke(此为默认值);
隐藏();
打破
案例Windows.System.VirtualKey.Escape:
结果=ContentDialogResult.Secondary;
SecondaryButtonClick?.Invoke(此为默认值);
隐藏();
打破
违约:
基元键合(e);
打破
}
}
}
这个问题已经问了很久了,但是ContentDialog有一个DefaultButton属性,可以按照您想要的方式处理Enter
对于文本框,我假设您必须将AcceptsReturn属性设置为false,因为这可能会干扰用于关闭对话框的Enter
谢谢,这确实有效。我想知道为什么Hide
不使用参数将ShowAsync
的ContentDialogResult
设置为与ContentDialogResult不同的值。在c.Hide()之前,一个可能的改进就是设置c.tag=ContentDialogResult.Primary。对于VirtualKey.Escape,设置c.tag=ContentDialogResult.Secondary。如果c.ShowAsync的结果是ContentDialogResult.None,那么可以推断结果是作为ContentDialogResult转换的对话框标记。太棒了,你真是个天才!VS不认可CaptionTB,也不建议使用。。。如何修复?这是我用来做标题的文本框。您将在XAML中定义为
Mahmouds解决方案并不完美。它仍然是新的功能,更重要的是:它改变了showsync
签名。如果是这样的话,为什么不使用一个完全不同的名称呢?不管怎样,你的答案有了开关
的改变,马哈茂德的答案基本上就是我的答案,稍微修改一下