【问题标题】:The name 'InitializeComponent' does not exist in the current context : strange behaviour当前上下文中不存在名称“InitializeComponent”:奇怪的行为
【发布时间】:2023-03-20 06:23:01
【问题描述】:

我创建了非常小的 WPF 应用程序并面临一个问题。我有以下课程。

Employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StaticResourceVsDynamicResource
{
    public class Employee
    {
        public string strName;
        public int nId;

        public Employee()
        {
            strName = "Default name";
            nId = -1;
        }

        public string Name
        {
            get{return strName;}set{strName = value;}
        }

        public int ID
        {
            get{return nId;}set{nId = value;}
        }
    }
}

MainWindow.xamal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StaticResourceVsDynamicResource
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["objEmployee"] = new Employee { Name = "Changed employee", ID = 100};

            this.Resources.Add("myBrush",new SolidColorBrush(SystemColors.GrayTextColor));            
        }
    }
}

MainWindow.xamal

<Window x:Class="StaticResourceVsDynamicResource.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StaticResourceVsDynamicResource"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>        
    <x:ArrayExtension Type="{x:Type sys:String}" x:Key="objNames">
        <sys:String>A1</sys:String>
        <sys:String>A2</sys:String>
    </x:ArrayExtension>
    <local:Employee x:Key="objEmployee"></local:Employee>
</Window.Resources>
<Grid>                       
    <Grid Height="100" HorizontalAlignment="Left" Margin="281,12,0,0" Name="grid3" VerticalAlignment="Top" Width="200" >           
        <ComboBox ItemsSource="{StaticResource ResourceKey=objNames}" Height="23" HorizontalAlignment="Left" Margin="48,37,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Grid>

上面的 xaml 代码很有趣。当我构建此代码时,我没有收到任何错误。无论出于何种原因,我只是随机调整&lt;x:ArrayExtension&gt;&lt;local:Employee&gt; 的位置,然后开始出现错误。

The name 'InitializeComponent' does not exist in the current context

当我在 &lt;x:ArrayExtenion&gt; 之前声明 &lt;local:Employee&gt; 时,只有我收到此错误。我确信这与命名空间有关,但我无法弄清楚。请参阅以下导致编译错误的代码。

<Window.Resources>
    <local:Employee x:Key="objEmployee"></local:Employee>
    <x:ArrayExtension Type="{x:Type sys:String}" x:Key="objNames">
        <sys:String>A1</sys:String>
        <sys:String>A2</sys:String>
    </x:ArrayExtension>        
</Window.Resources>

有人可以帮忙吗?似乎是一个奇怪的问题,但它是......

问候, 赫曼特

【问题讨论】:

标签: wpf


【解决方案1】:

我也遇到了同样的问题。我解决它的方法是将 XAML 文件的构建操作更改为 Page。

感谢我找到解决方案的来源: http://blog.mahop.net/post/Compile-Error-for-WPF-Files-The-name-InitializeComponent-does-not-exist-in-the-current-context.aspx

【讨论】:

  • 这个答案根本没有解决这个特定问题。实际上,默认情况下,WPF Window XAML 文件的构建操作已经是“Page”。
猜你喜欢
  • 2011-10-19
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
相关资源
最近更新 更多