简体   繁体   中英

Why is my XAML Code not running?

my C# Code works, but the XAML Code isn't working... I don't know what mistake I make. I'm a newbie on XAML and I try to learn it. When I delete this code:

KeyDown = "HandleKeyDown"

Initialized = "MainWindow_Initilized" Background ="DimGray">

then there is no error.

<Window x:Class="Tetris.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="570" Width="525">
    KeyDown = "HandleKeyDown" 
    Initialized = "MainWindow_Initilized" Background ="DimGray">

  <DockPanel LastChildFill="False">
    <StackPanel DockPanel.Dock="Right" Width="127">
      <Label Content="Label" Height="56" Name="Scores" FontSize="28" FontWeight="Bold" />
      <Label Content="Label" Height="56" Name="Lines" FontSize="28" FontWeight="Bold"/>
    </StackPanel>
    <Grid Name="MainGrid" Height="500" Width="250">
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
    </Grid>
  </DockPanel>
 </Window>

you have an extra > there right before you declare your KeyDown attribute.

Take note of there the color highlight stops

<Window x:Class="Tetris.MainWindow"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    Title="MainWindow" Height="570" Width="525">  
    KeyDown = "HandleKeyDown"   
    Initialized = "MainWindow_Initilized" Background ="DimGray"> 

I don't think you intend for that to be there.

Because you closed the Window tag twice:

Title="MainWindow" Height="570" Width="525">
KeyDown = "HandleKeyDown" 
Initialized = "MainWindow_Initilized" Background ="DimGray">

You closed it after Width="525" and again after Background ="DimGray" . Remove the one after Width="525" and it should be fine provided that you have </Window> at the very bottom of your Window XAML.

Also, if you are trying to use KeyDown to implement keyboard shortcuts, you should be doing something like this instead:

<Window.InputBindings>
<KeyBinding Gesture="Ctrl+O" Command="{commands:ApplicationCommand}" CommandParameter="OpenFile"/>
</Window.InputBindings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM