I created a very small WPF application and ran into one problem. I have classes below.
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 {
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>
The above xaml code is interesting. When I create this code, I did not get any errors. For some reason, I just shuffle the position of <x:ArrayExtension> and <local:Employee> , and I begin to get below the error.
The name 'InitializeComponent' does not exist in the current context
When I declare <local:Employee> before <x:ArrayExtenion> , then only I get this error. I'm sure this should do something with the namespace, but I can't figure it out. See the code that causes the compilation error below.
<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? This seems like a weird problem, but it ...
Regards, Hemant
source share