简体   繁体   中英

Reupload: Can't use XAML ResourceDictionary in a C# / WPF class library

Last one got closed because it's was supposedly a duplicate question.

Just to clarify before this one gets closed again: My application is a "Class Library", not a "WPF application" or something similar. That means the following:

  • I don't have an App.XML you would normally get when you make a WPF application
  • All topics I've seen on similar issues are using WPF applications. I've tried their fixes without any succes.

So this is a copy/paste from my last question:

For a Revit plugin I'm building a WPF dialog, with a view, logic, and now some styling.

Very quickly I came to the conclusion that putting all my styling in the is really messy (it just means I'm scrolling through my XAML half of my time). So I wanted to split my styling from my XAML code (like you would with HTML and CSS).

So I found out about the and how I should go about setting it up. I have my window, with this resource code:

<Window.Resources>
<local:LocationView x:Key="mainViewDataSource" />
<FontFamily x:Key="Ubuntu">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Ubuntu</FontFamily>
<FontFamily x:Key="FontAwesomeSolid">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Solid</FontFamily>
<FontFamily x:Key="FontAwesomeRegular">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Regular</FontFamily>

<ResourceDictionary x:Key="dictionary" Source="pack://application:,,,/Kalec.Enveo;component/src/WPF/Styles/Dictionary.xaml" />

And my Dictionaries:

Dictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="MergedDictionaries">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionaries/InputStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

and InputStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="SearchInputStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Height" Value="30"/>
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border CornerRadius="15" Background="White" BorderBrush="Transparent" x:Name="border">
                    <Grid>
                        <TextBox Text="{Binding Path=Text,
                                            RelativeSource={RelativeSource TemplatedParent}, 
                                            Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged}"
                             x:Name="textSource" 
                             Background="Transparent" 
                             Panel.ZIndex="2" />
                            <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
                                <TextBox.Style>
                                    <Style TargetType="{x:Type TextBox}">
                                        <Setter Property="Foreground" Value="Transparent"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                                <Setter Property="Foreground" Value="Gray"/>
                                                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                                                <Setter Property="VerticalContentAlignment" Value="Center"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="Red"/>
                        <Setter Property="Foreground" Value="Red" />

                    </Trigger>
                    <Trigger Property="IsFocused" Value="true">
                        <Setter Property="Foreground" Value="Blue" />
                        <Setter Property="BorderBrush" TargetName="border" Value="Blue"/>
                    </Trigger>
                    <DataTrigger Binding="{Binding Path=Text}" Value="">
                        <Setter Property="Text" Value="Vul een adres in"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The problem is that I can't seem to use the style on my textBox:

    <Window.Resources>
    <local:LocationView x:Key="mainViewDataSource" />
    <FontFamily x:Key="Ubuntu">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Ubuntu</FontFamily>
    <FontFamily x:Key="FontAwesomeSolid">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Solid</FontFamily>
    <FontFamily x:Key="FontAwesomeRegular">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Regular</FontFamily>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary  Source="pack://application:,,,/Kalec.Enveo;component/src/WPF/Styles/Dictionaries/InputStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

And finally the textbox:

<StackPanel Margin="10,10,0,0" Orientation="Vertical">
                <StackPanel Panel.ZIndex="10" Orientation="Horizontal" 
                    Name="SearchParameters">
                    <TextBox Width="220" Style="{DynamicResource SearchInputStyle}"/>
                    <Button FontFamily="{StaticResource FontAwesomeSolid}" 
                        Content="&#xf019;" Foreground="#31B192" Grid.ColumnSpan="2" 
                        Style="{DynamicResource LocationImport}"
                        Margin="10, 0, 0, 0"/>
                </StackPanel>

                <Label Name="ErrorMessage" Visibility="Visible"></Label>
                <StackPanel Orientation="Vertical" Visibility="Collapsed" 
                        Height="400" Width="300"
                        Name="AddressList"></StackPanel>
            </StackPanel>

I simply get the error that it could not be resolved. I looked at some solutions, and most of them tell me to include the ResourceDictionary in my App.xaml, but I don't have that file, because my project is a class library (it's a requirement for the plugin).

Can I even use Resource Dictionaries? Or is there some other way to seperate the styling or XAML in different files?

If all your controls and resources are in the same library just put the relative path to the dictionary in the Source tag without the pack URI syntax.

For example if the Window is in the src/WPF folder and the Resource dictionary is src/WPF/Styles/Dictionaries/InputStyles.xaml .

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary  Source="Styles/Dictionaries/InputStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Here is also a link to a simmilar post about using dictionaries in a library.

If this does not solve your issue you need to provide more information on how you use the library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM