2

我正在使用我自己的类,派生自 ContentControl(我们称之为 MyControl) 我希望用户能够订阅我的控件的 KeyDown 和 KeyUp 事件。

我遇到的问题是 MyControl 似乎没有收到任何关键事件。我推断这可能是一个焦点问题。例如,如果我在我的项目中添加一个以编程方式将焦点设置在 MyControl 上的按钮,那么我将开始接收这些事件。问题是,只要我点击任何地方,包括在我自己的控件上,它似乎就会失去焦点(无论哪种方式,键都不再起作用了)。我也无法通过单击将其取回,但似乎我必须使用 MyControl.Focus 方法。

所以,我的问题是:如何使控件能够通过单击它来接受焦点?


此外,为了您的享受,我在下面添加了一个小示例项目来演示我遇到的问题:

主页.xaml

<Page
x:Class="Focus.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Focus"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="300"/>
    </Grid.ColumnDefinitions>
    <Grid x:Name="BG">

    </Grid>
    <StackPanel Grid.Column="1" Orientation="Vertical">
        <StackPanel Orientation="Horizontal">  
            <Button Content="Focus" Click="FocusClick"/>
        </StackPanel>
        <TextBlock x:Name="InfoText" FontSize="16" TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </StackPanel>
</Grid>
</Page>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Focus
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{

    public static MainPage Current;
    public MyControl ctrl;

    public MainPage()
    {
        this.InitializeComponent();
        Current = this;
        ctrl = new MyControl();
        BG.Children.Add(ctrl);

        ctrl.KeyDown += ctrl_KeyDown;
        ctrl.KeyUp += ctrl_KeyUp;
    }

    void ctrl_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        ShowInfo("Pressed " + e.Key);
    }

    private void ctrl_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        ShowInfo("Released " + e.Key);
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    public void ShowInfo(string text)
    {
        InfoText.Text = text + Environment.NewLine + InfoText.Text;
    }

    private void FocusClick(object sender, RoutedEventArgs e)
    {
        ctrl.Focus(FocusState.Programmatic);
    }
}
}

我的控制.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml;

namespace Focus
{
public class MyControl : ContentControl
{

    public MyControl()
        : base()
    {
        LinearGradientBrush lb = new LinearGradientBrush();
        lb.StartPoint = new Point(0, 0);
        lb.EndPoint = new Point(1, 1);
        lb.GradientStops.Add(new GradientStop() { Color = Colors.Red, Offset = 0, });
        lb.GradientStops.Add(new GradientStop() { Color = Colors.Yellow, Offset = 1, });

        Rectangle rect = new Rectangle();
        rect.Width = 800;
        rect.Height = 1000;
        rect.Fill = lb;
        this.Content = rect;
    }
}
}

这是通过一个名为 Focus 的空白项目完成的。上面的 3 个文件分别是 MainPage.xaml、MainPage.xaml.cs 和 MyControl.cs。

4

1 回答 1

0

我已经运行了您的示例,并且似乎 WinRT 中存在一些错误,您可以阅读此MSDN ThreadLostFocus作为一种解决方法,您可以添加对您控制的事件的订阅:

ctrl.LostFocus += ctrl_LostFocus;
void ctrl_LostFocus(object sender, RoutedEventArgs e)
{
    ((MyControl)sender).Focus(FocusState.Programmatic);
}

并在每次丢失时手动将焦点设置为您的控件。但对我来说,这是一个糟糕的解决方案,因为您的控制将始终集中。当然你可以扩展这个方法来提供一些额外的逻辑。

于 2012-11-15T11:52:03.473 回答