Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';不要在C#/WPF类库中使用XAML ResourceDictionary_C#_Wpf_Xaml - Fatal编程技术网

Can';不要在C#/WPF类库中使用XAML ResourceDictionary

Can';不要在C#/WPF类库中使用XAML ResourceDictionary,c#,wpf,xaml,C#,Wpf,Xaml,对于Revit插件,我正在构建一个WPF对话框,其中包含视图、逻辑和一些样式 很快我就得出结论,把我所有的样式放在一个文件夹中真的很混乱(这只意味着我有一半的时间在浏览我的XAML)。所以我想从XAML代码中分离我的样式(就像HTML和CSS一样) 所以我发现了这个问题,以及我应该如何设置它。我有我的窗口,带有以下资源代码: <Window.Resources> <local:LocationView x:Key="mainViewDataSource" /&g

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

pack://application:,,,/Kalec.Enveo;组件/src/Resources/font/#Ubuntu
pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5免费实体
pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5免费常规
还有我的字典:

字典.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>
<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>

InputStyles.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>
<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分离到不同的文件中?

我猜您得到了正确的成分,但没有得到正确的配方

以下是我的工作方式:

我有一个类库项目,它定义了我的所有样式(图中的ControlLibrary) 这是一本单键词典

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

这是所有个人风格的单一接触点。Dictionary.xaml (请注意,这是使用MergedDictionary)


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

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

我想你的配料是正确的,但食谱不正确

以下是我的工作方式:

我有一个类库项目,它定义了我的所有样式(图中的ControlLibrary) 这是一本单键词典

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

这是所有个人风格的单一接触点。Dictionary.xaml (请注意,这是使用MergedDictionary)


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

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


回答得不错,很抱歉,在找到问题之前我没有看到它duplicate@MichaelRandall-没问题:)谢谢,我错过了在Window.Resources中再次使用合并词典。我会试试看,然后再打给你!对不起,它还是不起作用。另外,我使用的样式应用程序不是WPF应用程序,而是C#类库。@Martijn-这就是问题所在。您需要首先将项目添加为WPF应用程序。这会添加所有相关引用(PresentationCore、WindowsBase等),如果我没有弄错的话,还会在AssemblyInfo.cs中添加一个内容。这允许进行适当的编译等。一旦组织好项目结构(删除app.config、app.xaml等),然后可以将项目类型更改为ClassLibrary(我就是这么做的)。回答不错,很抱歉,在找到duplicate@MichaelRandall-没问题:)谢谢,我错过了在Window.Resources中再次使用合并词典。我会试试看,然后再打给你!对不起,它还是不起作用。另外,我使用的样式应用程序不是WPF应用程序,而是C#类库。@Martijn-这就是问题所在。您需要首先将项目添加为WPF应用程序。这会添加所有相关引用(PresentationCore、WindowsBase等),如果我没有弄错的话,还会在AssemblyInfo.cs中添加一个内容。这允许进行适当的编译等。一旦组织好项目结构(删除app.config、app.xaml等),就可以将项目类型更改为ClassLibrary(我就是这么做的)。