2009-03-31 9 views
0

ItemsSource = "{DynamicResource testResource}"のComboBoxがあります。 testResourceはC#コードで設定したアプリケーションリソースです。アプリケーションリソース割り当ての詳細を理解しようとしています

私が気づいたことは、私は窓をロードする場合、アプリケーションが作成したbefor、リソースがコンボボックスによってロードされていないということです。

Window window = (Window)LoadXaml("Window1.xaml"); 
Application app = new Application(); 

このコードは

また
Application app = new Application(); 
Window window = (Window)LoadXaml("Window1.xaml"); 

を動作しますが、私が作成した場合でも、ウィンドウbeforアプリケーション、私はボタンクリックハンドラで後者のリソースを読み込むことができます。

何か説明できますか?注文が重要な理由

Window1.xaml:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
     <ComboBox ItemsSource="{DynamicResource testResource}" SelectedIndex="0"></ComboBox> 
     <Button x:Name='testButton'>Test</Button> 
    </StackPanel> 
</Window> 

C#

class Program 
{ 
    [STAThread] 
    public static void Main() 
    { 
     Window window = (Window)LoadXaml("Window1.xaml"); 
     Application app = new Application(); 
     SetupResource(); 

     (window.FindName("testButton") as Button).Click += new RoutedEventHandler(testButton_Click); 
     window.Show(); 
     app.Run(window); 
    } 

    static void testButton_Click(object sender, RoutedEventArgs e) 
    { 
     SetupResource(); 
    } 

    static void SetupResource() 
    { 
     List<string> list = new List<string>(); 
     list.Add("Hola"); 
     list.Add("Mundo"); 
     Application.Current.Resources["testResource"] = list; 
    } 

    static object LoadXaml(string fileName) 
    { 
     return XamlReader.Load(File.Open(fileName, FileMode.Open)); 
    } 
} 

答えて

0

わからないが、Applicationオブジェクトが作成されたときにApplicationのリソースのみがロードされているので、私は推測します。したがって、testResourceにアクセスする場合は、new Application()への呼び出し後に行う必要があります。

関連する問題