页面上的WPF KeyDown事件 [英] WPF KeyDown event on page

查看:132
本文介绍了页面上的WPF KeyDown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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但我需要使用Page s时,它可以工作.

The problem is that KeyDown event doesn't work however MouseLeftButtonDown event works well. How can I solve it? It worked when I used normal Window but I need to use Pages.

推荐答案

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

You maybe need to use PreviewKeyDown on a Page

+=new PreviewKeyDownEventHandler(MainWindow_PreviewKeyDown);

代替

+= new KeyEventHandler(MainWindow_KeyDown);

这只是一个主意,我不确定. (而且我无法测试)

It's just an idea, I'm not sure. (And I can't test it)

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

It's seems that you need to attach your event using the window

win.KeyDown += new KeyEventHandler(MainWindow_KeyDown);

这篇关于页面上的WPF KeyDown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆