【问题标题】:Create Application from code and load resource dictionnary from xaml从代码创建应用程序并从 xaml 加载资源字典
【发布时间】:2012-09-05 13:52:46
【问题描述】:

这可能是一个非常愚蠢的问题,但我现在根本无法理解这个问题。

我有一个定义应用程序级资源的 App.xaml 文件:

<Application x:Class="WpfMPManager.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyDictionnary.xaml"/>                   
    </ResourceDictionary>
</Application.Resources>

出于各种原因,我通过一个类似的 .cs 文件启动这个应用程序(实际上这个文件中还有很多内容)。

   [STAThread]
    static void Main(params string[] args)
    {
        App myApp = new App();
        MainWindow myWindow = new MainWindow();
        myApp.Run(myWindow);
     }

但是,当我以这种方式启动应用程序时,我的应用程序资源字典是空的(如果我通过 App.xml 启动应用程序,它们确实被填满)。

我应该调用 Application 对象上的特定方法来强制它加载 .xaml 文件中定义的资源字典吗?

提前致谢。

【问题讨论】:

    标签: c# .net wpf xaml resourcedictionary


    【解决方案1】:

    回答我自己的问题,以防有人偶然发现

    似乎调用默认构造函数不调用的myApp.InitializeComponent() 就足够了。本来可以更快地弄清楚的。

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多