【问题标题】:Can't use XAML ResourceDictionary in a C# / WPF class library [duplicate]无法在 C#/WPF 类库中使用 XAML ResourceDictionary [重复]
【发布时间】:2020-02-12 21:28:28
【问题描述】:

对于 Revit 插件,我正在构建一个 WPF 对话框,其中包含视图、逻辑和一些样式。

很快我就得出结论,将我所有的样式都放入其中是非常混乱的(这只是意味着我有一半时间在滚动浏览我的 XAML)。所以我想将我的样式从我的 XAML 代码中分离出来(就像使用 HTML 和 CSS 一样)。

所以我发现了 以及我应该如何设置它。我有我的窗口,有这个资源代码:

    <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" />

</Window.Resources>

还有我的字典:

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>

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>

问题是我似乎无法在我的文本框上使用该样式:

 <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>

我只是收到无法解决的错误。我查看了一些解决方案,其中大多数都告诉我在我的 App.xaml 中包含 ResourceDictionary,但我没有那个文件,因为我的项目是一个类库(这是插件的要求)。

我什至可以使用资源字典吗?还是有其他方法可以在不同的文件中分隔样式或 XAML?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    我猜你得到了正确的成分,但没有正确的食谱。

    它是如何为我工作的:

    我有一个类库项目,它定义了我的所有样式(图片中的 ControlLibrary) 这是一个 Button 样式 ResourceDictionary

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfApp1">
    
        <Style x:Key="RedButton" TargetType="Button">
            <Style.Setters>
                <Setter Property="Background" Value="Red"/>
            </Style.Setters>
        </Style>
    
        <Style x:Key="GreenButton" TargetType="Button">
            <Style.Setters>
                <Setter Property="Background" Value="Green"/>
            </Style.Setters>
        </Style>
    </ResourceDictionary>
    

    那么这里是所有个人风格的单一联系点。字典.xaml (注意这里使用的是 MergedDictionary)

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfApp1">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="./Styles/ButtonStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    最后为了在我的 WpfApp 项目中使用 ClassLibrary dll 中的样式(添加对它的引用之后),我们再次使用 MergedDictionaries..!!

    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/ControlLibrary;component/Dictionary.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
    

    【讨论】:

    • 不错的答案,抱歉我在找到重复项之前没有看到它
    • @MichaelRandall - 没问题 :)
    • 谢谢,我错过了在 Window.Resources 中再次使用 MergedDictionaries。我会尝试并回复您!
    • 很抱歉,它仍然无法正常工作。此外,我使用样式的应用程序不是 WPF 应用程序,它是 C# 类库。
    • @Martijn - 这就是问题所在。您需要首先将项目添加为 WPF App。如果我没记错的话,这会添加所有相关的引用(PresentationCore、WindowsBase 等)并且还会在 AssemblyInfo.cs 中添加一个东西。这允许正确编译等。一旦您组织了项目结构(删除 app.config、app.xaml 等),您就可以将项目类型更改为 ClassLibrary(这就是我所做的)。
    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多