我的C#代码有效,但XAML代码无效...我不知道我犯了什么错误。我是XAML的新手,我试着学习它。当我删除此代码时:
KeyDown =“HandleKeyDown”
Initialized =“MainWindow_Initilized”Background =“DimGray”>
然后没有错误。
<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>
答案 0 :(得分:3)
在您声明>
属性之前,您还有一个额外的KeyDown
。
注意颜色突出显示停止
<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">
我认为你并不打算那样做。
答案 1 :(得分:1)
因为您关闭了Window标记两次:
Title="MainWindow" Height="570" Width="525">
KeyDown = "HandleKeyDown"
Initialized = "MainWindow_Initilized" Background ="DimGray">
您在Width="525"
之后和Background ="DimGray"
之后再次关闭了它。删除Width="525"
之后的那个,如果您在Window XAML的最底部有</Window>
,则应该没问题。
此外,如果您尝试使用KeyDown
来实现键盘快捷键,则应该执行以下操作:
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+O" Command="{commands:ApplicationCommand}" CommandParameter="OpenFile"/>
</Window.InputBindings>