简体   繁体   English

重新上传:无法在 C#/WPF 类库中使用 XAML ResourceDictionary

[英]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.只是在再次关闭之前澄清一下:我的应用程序是“类库”,而不是“WPF 应用程序”或类似的东西。 That means the following:这意味着以下内容:

  • I don't have an App.XML you would normally get when you make a WPF application我没有您在制作 WPF 应用程序时通常会得到的 App.XML
  • All topics I've seen on similar issues are using WPF applications.我在类似问题上看到的所有主题都使用 WPF 应用程序。 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.对于 Revit 插件,我正在构建一个 WPF 对话框,其中包含视图、逻辑和一些样式。

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).所以我想从我的 XAML 代码中分离我的样式(就像你对 HTML 和 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字典.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和 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).我查看了一些解决方案,其中大多数告诉我在 App.xaml 中包含 ResourceDictionary,但我没有那个文件,因为我的项目是一个类库(这是插件的要求)。

Can I even use Resource Dictionaries?我什至可以使用资源词典吗? Or is there some other way to seperate the styling or XAML in different files?或者是否有其他方法可以在不同的文件中分离样式或 XAML?

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.如果您的所有控件和资源都在同一个库中,只需将字典的相对路径放在 Source 标记中,而无需包 URI 语法。

For example if the Window is in the src/WPF folder and the Resource dictionary is src/WPF/Styles/Dictionaries/InputStyles.xaml .例如,如果 Window 位于src/WPF文件夹中,而 Resource 字典是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.如果这不能解决您的问题,您需要提供有关如何使用该库的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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