C# 无法识别WPF InitializeComponent

C# 无法识别WPF InitializeComponent,c#,wpf,C#,Wpf,对于WPF来说,这是一个全新的概念,并且在Microsoft演练中要求将XML替换为以下内容: <Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Northwind Orders" Height="335" Wi

对于WPF来说,这是一个全新的概念,并且在Microsoft演练中要求将XML替换为以下内容:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Northwind Orders" Height="335" Width="425" 
        Name="OrdersWindow" Loaded="Window1_Loaded">
    <Grid Name="orderItemsGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="258*"/>
            <ColumnDefinition Width="161*"/>
        </Grid.ColumnDefinitions>
        <ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}"
                  IsSynchronizedWithCurrentItem="true" 
                  Height="23" Margin="92,12,37.703,0" Name="comboBoxOrder" VerticalAlignment="Top"/>
        <DataGrid ItemsSource="{Binding Path=Order_Details}"  
                  CanUserAddRows="False" CanUserDeleteRows="False"  
                  Name="orderItemsDataGrid" Margin="34,46,34.4,49.6"
                  AutoGenerateColumns="False" Grid.ColumnSpan="2">
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Product" Binding="{Binding ProductID, Mode=OneWay}" />
                <DataGridTextColumn  Header="Quantity" Binding="{Binding Quantity, Mode=TwoWay}" />
                <DataGridTextColumn  Header="Price" Binding="{Binding UnitPrice, Mode=TwoWay}" />
                <DataGridTextColumn  Header="Discount" Binding="{Binding Discount, Mode=TwoWay}" />
            </DataGrid.Columns>
        </DataGrid>
        <Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top" 
               HorizontalAlignment="Left" Width="65">Order:</Label>
        <StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right" 
                    Height="40" Margin="0,261,22.4,4.6" Grid.ColumnSpan="2">
            <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" 
                Name="buttonSave" VerticalAlignment="Bottom" Width="75" 
                    Click="buttonSaveChanges_Click">Save Changes
            </Button>
            <Button Height="23" Margin="0,0,12,12" 
                Name="buttonClose" VerticalAlignment="Bottom" Width="75" 
                    Click="buttonClose_Click">Close</Button>
        </StackPanel>
    </Grid>
</Window>

订单:
保存更改
接近
所以我替换了它,但是现在如果我转到文件
MainWindow.xmal.cs
InitializeComponent()的代码被突出显示为不存在


这是为什么?我如何更正它?

您必须将XAML中的
x:Class=“Window1”
与类后代码
类主窗口相匹配


将XAML从
x:Class=“Window1”
更改为
x:Class=“NorthwindClient.MainWindow”
您必须将XAML中的
x:Class=“Window1”
与类后代码
类主窗口相匹配


将XAML从
x:Class=“Window1”
更改为
x:Class=“NorthwindClient.MainWindow”

该XAML中使用了两个事件处理程序,它们应该在代码隐藏(MainWindow.XAML.cs)中声明,如Window1\u加载、buttonSaveChanges\u单击和buttonClose\u单击。你不应该简单地复制一堆没有代码隐藏部分的XAML。@克莱门斯-我只是这样说的:XAML中也有
x:Class=“Window1”
,实际上应该是
x:Class=“NorthwindClient.MainWindow”
。我建议您在VisualStudio中重新开始一个新的WPF应用程序项目。还有一本介绍WPF的书。我最喜欢的是Adam Nathan的“WPF Unreleased”。@clemens谢谢你的参考,我刚买了那本书,因为WPF看起来像是很酷的技术,值得我花时间去研究。XAML中使用了两个事件处理程序,它们应该在代码隐藏(MainWindow.XAML.cs)中声明,比如Window1\u加载,buttonSaveChanges\u点击,和按钮关闭并单击。你不应该简单地复制一堆没有代码隐藏部分的XAML。@克莱门斯-我只是这样说的:XAML中也有
x:Class=“Window1”
,实际上应该是
x:Class=“NorthwindClient.MainWindow”
。我建议您在VisualStudio中重新开始一个新的WPF应用程序项目。还有一本介绍WPF的书。我最喜欢的是亚当·内森的《WPF释放》。@clemens谢谢你的参考,我刚买了那本书,因为WPF看起来很酷,值得我花时间学习。