0
votes

I have a NavigationWindow with Page:

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>

Page Code Behind

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

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.

1

1 Answers

1
votes

You maybe need to use PreviewKeyDown on a Page

+=new PreviewKeyDownEventHandler(MainWindow_PreviewKeyDown);

instead of

+= new KeyEventHandler(MainWindow_KeyDown);

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

[EDIT]

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

win.KeyDown += new KeyEventHandler(MainWindow_KeyDown);