单击ContentControl不会使其成为焦点

时间:2012-11-14 23:59:18

标签: c# windows-8 winrt-xaml

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

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

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


另外,为了您的乐趣,我在下面添加了一个小样本项目来演示我遇到的问题:

MainPage.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);
    }
}
}

MyControl.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。

1 个答案:

答案 0 :(得分:0)

我已经运行了您的示例,看起来WinRT中存在一些错误,您可以阅读此MSDN Thread。作为一种变通方法,您可以添加对您的控件LostFocus事件的订阅:

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

每次丢失时手动将焦点设置到控件上。 但对我而言,这是一个糟糕的解决方案,因为你的控制权总是会集中精力。当然,您可以扩展此方法以提供一些额外的逻辑。