UWP KeyboardAccelerator 的功能
UWP KeyboardAccelerator of functions
我知道如何将 KeyboardAccelerators
添加到 UIElement
中,但是没有 UIElement
它们可以存在吗?我只是想让键盘组合激活一个功能。
例如,我希望用户在按下右键时触发一个名为 moveFiveSecsAhead()
的函数,而在按下左键时触发一个名为 moveFiveSecsBack()
的函数。
我怎样才能做到这一点?我能想到的一种可能的解决方案是添加两个不可见按钮。但是有更简单的解决方案吗?
---更新---
所以我将这段代码添加到我的control,它似乎在调试模式下运行良好但在发布模式下导致崩溃。这是为什么?
var left = new KeyboardAccelerator() { Key = Windows.System.VirtualKey.Left };
left.Invoked += (sender, args) => MediaHelper.Position = Math.Max(MediaHelper.Position - 5, 0);
var right = new KeyboardAccelerator() { Key = Windows.System.VirtualKey.Right };
right.Invoked += (sender, args) => MediaHelper.Position = Math.Min(MediaHelper.Position + 5, CurrentMusic.Duration);
this.KeyboardAccelerators.Add(left);
this.KeyboardAccelerators.Add(right);
您可以将键盘加速器添加到您正在使用的 Page
,而无需添加两个不可见按钮。
MainPage.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var accelerator = new KeyboardAccelerator() { Key = , Modifiers= };
accelerator.Invoked += moveFiveSecsBack;
var accelerator1 = new KeyboardAccelerator() { Key = , Modifiers = };
accelerator.Invoked += moveFiveSecsAhead;
this.KeyboardAccelerators.Add(accelerator);
this.KeyboardAccelerators.Add(accelerator1);
}
private void moveFiveSecsBack(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
throw new NotImplementedException();
}
private void moveFiveSecsAhead(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
throw new NotImplementedException();
}
}
希望对您有所帮助。
我知道如何将 KeyboardAccelerators
添加到 UIElement
中,但是没有 UIElement
它们可以存在吗?我只是想让键盘组合激活一个功能。
例如,我希望用户在按下右键时触发一个名为 moveFiveSecsAhead()
的函数,而在按下左键时触发一个名为 moveFiveSecsBack()
的函数。
我怎样才能做到这一点?我能想到的一种可能的解决方案是添加两个不可见按钮。但是有更简单的解决方案吗?
---更新---
所以我将这段代码添加到我的control,它似乎在调试模式下运行良好但在发布模式下导致崩溃。这是为什么?
var left = new KeyboardAccelerator() { Key = Windows.System.VirtualKey.Left };
left.Invoked += (sender, args) => MediaHelper.Position = Math.Max(MediaHelper.Position - 5, 0);
var right = new KeyboardAccelerator() { Key = Windows.System.VirtualKey.Right };
right.Invoked += (sender, args) => MediaHelper.Position = Math.Min(MediaHelper.Position + 5, CurrentMusic.Duration);
this.KeyboardAccelerators.Add(left);
this.KeyboardAccelerators.Add(right);
您可以将键盘加速器添加到您正在使用的 Page
,而无需添加两个不可见按钮。
MainPage.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var accelerator = new KeyboardAccelerator() { Key = , Modifiers= };
accelerator.Invoked += moveFiveSecsBack;
var accelerator1 = new KeyboardAccelerator() { Key = , Modifiers = };
accelerator.Invoked += moveFiveSecsAhead;
this.KeyboardAccelerators.Add(accelerator);
this.KeyboardAccelerators.Add(accelerator1);
}
private void moveFiveSecsBack(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
throw new NotImplementedException();
}
private void moveFiveSecsAhead(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
throw new NotImplementedException();
}
}
希望对您有所帮助。