【问题标题】:Where to place Resources在哪里放置资源
【发布时间】:2018-09-21 14:14:20
【问题描述】:

在我的 WPF 应用程序中,我在 App.xaml 中定义了一个 Style 并在其他页面上使用它。但我想将我的项目编译成 .dll,这需要我删除 App.xaml。所以我需要将我的Style 放在其他地方(最好是在全球范围内,而不是在我制作的每个页面中)。我可以放在哪里以及如何使用它?

顺便说一句,我正在使用 MvvmCross,但我认为这无关紧要。

【问题讨论】:

标签: wpf


【解决方案1】:

基本上,您可以在任何您认为合适的地方创建一个包含<ResourceDictionary> 的新 xaml,然后您可以将这些资源字典加载到任何您想要的位置:

 <Page.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="myresourcedictionary.xaml"/>
      <ResourceDictionary Source="myresourcedictionary2.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Page.Resources>

更多详情here

【讨论】:

    【解决方案2】:

    对于全局声明,如果您看到为 App.Xaml 生成的 cs 文件,您将看到类似于 windowsForm 的 Program.cs 文件(与 Form 部分类代码混合)。

    这是该文件的一部分:(App.i.g.cs)

        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
    
            #line 7 "..\..\App.xaml"
            this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
    
            #line default
            #line hidden
            System.Uri resourceLocater = new System.Uri("/WpfPlayground;component/app.xaml", System.UriKind.Relative);
    
            #line 1 "..\..\App.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);
    
            #line default
            #line hidden
        }
    

    实际上,下面的行将强制应用程序将您的特定资源字典文件用作全局资源字典。

            System.Windows.Application.LoadComponent(this, resourceLocater);
    

    如果您创建一个符合 App.xaml 文件的文件(您知道这是您的 EXE 文件的主要入口点),则无需依赖此文件。

    注意: App.i.g.cs 是自动生成的,您必须遵守 App.XAML 规则。

    【讨论】:

      猜你喜欢
      • 2012-01-25
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多