gpt4 book ai didi

c# - 单击 ContentControl 不会为其提供焦点

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:46 25 4
gpt4 key购买 nike

我正在使用我自己的类,派生自 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。

最佳答案

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

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

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

关于c# - 单击 ContentControl 不会为其提供焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13389508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com