0

我有NavigationWindow一个Page

页面 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.

4

1 回答 1

1

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

+=new PreviewKeyDownEventHandler(MainWindow_PreviewKeyDown);

代替

+= new KeyEventHandler(MainWindow_KeyDown);

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

[编辑]

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

win.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
于 2013-08-12T08:37:59.793 回答