【问题标题】:WPF KeyDown event on page页面上的 WPF KeyDown 事件
【发布时间】:2013-08-16 00:02:24
【问题描述】:

我有一个NavigationWindowPage

页面 XAML:

<Page x:Class="Existence.IntroPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" ShowsNavigationUI="False"
    d:DesignHeight="300" d:DesignWidth="300"
    Title="IntroPage" Loaded="Page_Loaded">
    <Grid Name="gridzik">
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Rectangle Name="aa" Fill="Black" Grid.RowSpan="3">
            <Rectangle.Triggers>
                <EventTrigger RoutedEvent="Window.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="img" Storyboard.TargetProperty="Opacity" From="0" To="1" BeginTime="0:0:1" Duration="0:0:2" AutoReverse="False"/>
                            <DoubleAnimation Storyboard.TargetName="img2" Storyboard.TargetProperty="Opacity" From="0" To="1" BeginTime="0:0:3" Duration="0:0:2" AutoReverse="False" Completed="DoubleAnimation_Completed"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
        <Image Name="img" Source="X:\Densetsu Existence\wstepne logo existence1.jpg" Grid.RowSpan="2" Opacity="0"></Image>
        <Image Name="img2" Source="X:\Densetsu Existence\wstepne logo existence2.jpg" Grid.RowSpan="2" Opacity="0"></Image>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Viewbox x:Name="vbBig" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock Name="ee" Visibility="Hidden" Text="Press Enter" Foreground="White" FontWeight="DemiBold">
                </TextBlock>
            </Viewbox>
        </Grid>
    </Grid>
</Page>

页面代码隐藏

public partial class IntroPage : Page
    {
        NavigationWindow win;
        public double max, min;
        public DoubleAnimation da;
        public IntroPage()
        {
            InitializeComponent();
        }
        private void DoubleAnimation_Completed(object sender, EventArgs e)
        {
            ee.Visibility = Visibility.Visible;
            this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
            max = win.Height / 5;
            min = max / 2;
            da = new DoubleAnimation();
            da.From = min;
            da.To = max;
            da.Duration = new Duration(TimeSpan.FromSeconds(1));
            da.AutoReverse = true;
            da.RepeatBehavior = RepeatBehavior.Forever;
            vbBig.BeginAnimation(Button.HeightProperty, da);
        }
        public void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                win.Navigate(new MainMenuPage());
        }
        public void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            img2.Visibility = Visibility.Hidden;
            win.Navigate(new MainMenuPage());
        }
        public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            max = ((NavigationWindow)sender).Height / 5;
            min = max / 2;
            if (da != null)
            {
                vbBig.BeginAnimation(Button.HeightProperty, null);
                da.From = min;
                da.To = max;
                vbBig.BeginAnimation(Button.HeightProperty, da);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            win = (NavigationWindow)Window.GetWindow(this);
            win.SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
        }
    }

问题是KeyDown 事件不起作用,但MouseLeftButtonDown 事件运行良好。我该如何解决?当我使用普通的Window 时它可以工作,但我需要使用Pages。

【问题讨论】:

    标签: wpf keydown


    【解决方案1】:

    您可能需要在页面上使用 PreviewKeyDown

    +=new PreviewKeyDownEventHandler(MainWindow_PreviewKeyDown);
    

    而不是

    += new KeyEventHandler(MainWindow_KeyDown);
    

    这只是一个想法,我不确定。 (而且我无法测试)

    [编辑]

    您似乎需要使用窗口附加您的活动

    win.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
    

    【讨论】:

    • 我更新了答案。看看我找到的解决方案,我对其进行了测试,它对我有用。
    • 但是在下一页我需要删除它吗?我可以以某种方式检查处理程序是否有任何事件或只是清除它?并感谢您的帮助
    • 这可以帮助您弄清楚如何处理它:stackoverflow.com/a/4094637/1408558
    猜你喜欢
    • 2020-07-05
    • 2010-09-25
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多