当前上下文中不存在名称“InitializeComponent":奇怪的行为 [英] The name 'InitializeComponent' does not exist in the current context : strange behaviour

查看:35
本文介绍了当前上下文中不存在名称“InitializeComponent":奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created very small WPF application and facing one problem. I have below classes.

员工.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

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

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 代码很有趣.当我构建此代码时,我没有收到任何错误.无论出于何种原因,我只是调整 的位置,然后我开始出现以下错误.

Above xaml code is intresting. When I build this code I didn't get any error. For whatever reason I just shuffle position of <x:ArrayExtension> and <local:Employee> and I start getting below error.

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

当我在 之前声明 时,只有我收到此错误.我确信这与命名空间有关,但我无法弄清楚.请参阅以下导致编译错误的代码.

When I am declaring <local:Employee> before <x:ArrayExtenion> then only I am getting this error. I am sure this has to do something with namespace, but I am not able to figure it out. See the below code which is causing compilation error.

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

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

Can anyone help? Seems to be a strange problem but it is...

问候,曼特

推荐答案

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

I've had the same problem. The way I resolved it was by changing the build action of the XAML file to Page.

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

To credit the source where I found the solution: http://blog.mahop.net/post/Compile-Error-for-WPF-Files-The-name-InitializeComponent-does-not-exist-in-the-current-context.aspx

这篇关于当前上下文中不存在名称“InitializeComponent":奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆