在 UWP C# 中调整焦点网格元素的大小

Resizing focused Grid element in UWP C#

我有这个应用程序,我可以在其中插入图像并移动它们。

我想要的是当某些网格聚焦在键盘上的 Up/Down 箭头时能够调整它们的大小。

我试过将此功能添加到 KeyDown 中,但它不起作用

UIElement_OnKeyDown(object sender, KeyRoutedEventArgs e)

之后我在网上看到一些评论并尝试

Window.Current.CoreWindow.KeyDown += UIElement_OnDownKey;

但我不知道如何通过这个以函数为中心的元素并调整它的大小,因为我最终得到

CoreWindow sender, KeyEventArgs e

元素

谢谢!

您可以(甚至应该)使用 FocusManager。要在简单的情况下使用它,只要您 click/tap 在某个对象上,您只需将直接焦点设置在该元素上。然后在设置焦点在该元素上之后,您可以使用检索焦点元素并对其进行操作。

通过添加

设法实现了我的意图
private async void UIElement_OnDownKey(CoreWindow sender, KeyEventArgs e)
{
    Image myImage = GetFrontImage();
    switch(e.VirtualKey)
    {
        case Windows.System.VirtualKey.Down: //Same thing for Up. 
        {
             myImage.Width = myImage.Width * 0.98;
             myImage.Height = myImage.Height * 0.98;
             break;
        }
    }
}

GetFrontImage() 获取鼠标光标下的图像,我设置如下:

img.PointerEntered += setFocuse; // setFocuse function sets this image to be the one returned by GetFrontImage() function