Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wpf - The name 'InitializeComponent' does not exist in the current context : strange behaviour

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

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>

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

Regards, Hemant

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...