Skip to main content
 首页 » 编程设计

c#之为什么我的 XAML 代码没有运行

2024年02月13日12qq号

我的 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> 

请您参考如下方法:

在声明 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">  

我不认为你打算在那里。