2013-04-18 21 views
3

我已經創建了非常小的WPF應用程序和麪臨的一個問題。我有下面的課程。「的InitializeComponent」並不在當前的背景下存在的名稱:奇怪的行爲

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代碼intresting。當我建立這個代碼時,我沒有得到任何錯誤。無論出於何種原因,我只是洗牌<x:ArrayExtension><local:Employee>的位置,我開始出現錯誤。

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

當我聲明<local:Employee><x:ArrayExtenion>在此之前只有我得到這個錯誤。我相信這必須用命名空間做些事情,但我無法弄清楚。請參閱下面導致編譯錯誤的代碼。

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

任何人都可以幫忙嗎?似乎是一個奇怪的問題,但它是...

問候, 與Hemant

+0

當我創建一個全新的WPF項目我得到這個相同的錯誤消息。它不會編譯或運行。 – Jeff

+1

請參閱[XAML無法編譯,但沒有任何錯誤消息...](http://stackoverflow.com/q/30879531)。相同的基本設置。包括鏈接到連接錯誤報告。 –

回答

相關問題