ResourceDictionary in separate library

后端 未结 3 856
悲哀的现实
悲哀的现实 2020-12-22 11:30

I have defined my resourceDictionary in a separate library as below



        
相关标签:
3条回答
  • 2020-12-22 12:14

    You need to provide a reference to the ResourceDictionary either in the app.xaml file or locally. Resources in app.xaml are available globally to all xaml files of the application.

    For Xaml files in library projects the designer works a little differently.

    At runtime it will be the app.xaml in the startup project that will be used for all assemblies. At designtime it will be the app.xaml of the local assembly. This means, you can add a app.xaml file to libraies, which will only be used by the Visual Studio Designer when rendering xaml files from that specific library (Set build action of file to Page).

    To reference a ResourceDictionary do this:

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <Application.Resources>
            <ResourceDictionary>
    
                <!-- Other global resources -->
    
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/ASSEMBLYNAME;component/FOLDERPATH/RDNAME.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    Where ASSEMBLYNAME is the name of the assembly where the ResourceDictionary is (check properties of project).

    Example:

    Project with assembly name: "MyAssembly" and ResourceDictionary in folder path "Resources/RDs/MyRd.xaml"

    Source="pack://application:,,,/MyAssembly;component/Resources/RDs/MyRd.xaml"
    
    0 讨论(0)
  • 2020-12-22 12:27

    In App.xaml, you must add a MergedDictionary to the resources which will reference your other dictionary.

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/RibbonControlsLibrary;component/Your/Dictionary/Path.xaml" />
                ...
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
    0 讨论(0)
  • 2020-12-22 12:34

    You can include the ResourceDictionary in each xaml

    <msRibbon:RibbonTab x:Class="Ribbon.Planner.PlannerRibbon"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:msRibbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        mc:Ignorable="d" 
        d:DesignHeight="100" d:DesignWidth="500" Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}">
        <msRibbon:RibbonTab.Resources>
            <ResourceDictionary Source="pack://application:,,,/<ASSEMBLY_NAME>;component/<RESOURCES_FILE>.xaml"/>
        </msRibbon:RibbonTab.Resources>
        <Grid>
         ...
         ...
         ...
        </Grid>
    
    0 讨论(0)
提交回复
热议问题