尝试了解应用程序资源分配的详细信息

发布于 2024-07-16 17:59:25 字数 1789 浏览 3 评论 0原文

我有一个带有 ItemsSource="{DynamicResource testResource}" 的组合框。 testResource 是我在 C# 代码中设置的应用程序资源。

我注意到,如果我在创建应用程序之前加载窗口,则 ComboBox 不会加载资源:

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

此代码有效

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

另外,即使我在应用程序之前创建了窗口,我也可以在按钮单击处理程序中加载资源。

有人可以解释一下,会发生什么吗? 为什么顺序很重要?

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));
    }
}

I have a ComboBox with ItemsSource="{DynamicResource testResource}". The testResource is the Application resource that I set in C# code.

What I have noticed is that if I load Window befor Application created, the resource is not loaded by ComboBox:

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

This code works

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

Also, even if I created the window befor the application, I can load resource latter in button click handler.

Can some one explain, what happens? Why the order matters?

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));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅士风度i 2024-07-23 17:59:25

不确定,但我猜是因为 Application 的资源仅在创建 Application 对象时加载。 因此,如果您想访问testResource,您需要在调用new Application()之后进行。

Not sure, but I would guess because the Application's Resources are only loaded when the Application object is created. So if you want to access testResource, you need to do it after the call to new Application().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文