【问题标题】:WPF Class Library with custom control - control won't show带有自定义控件的 WPF 类库 - 控件不会显示
【发布时间】:2013-10-14 15:34:06
【问题描述】:

在网上搜索了几个小时后,我向大家求助:

我的 WPF 类库(.NET 3.5,COM 可见)中有一个表单,它使用一个 UserControl 和一个主题文件。

问题: 使用 WPF 应用程序时,这些按钮工作得很好,但在这个 .NET 3.5、COM Visible、类库中,它们根本不会显示。库中的其他对象可以使用和工作。 可能是什么问题呢?我偏向于找不到的资源字典,或者一些找不到的资源,但我不能指手画脚。 欢迎任何帮助!

一些代码

资源是通过设置的

<Window.Resources>
   <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary  Source="pack://application:,,,/MyLibrary;component/Themes/Generic.xaml"/>
       </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</Window.Resources>

我们在这里放置 - 现在 - 一个简单的:

<Grid>
   <lib:ImageButton ImageSource="/MyLibrary;component/Images/some_image.png" />
</Grid>

不要忘记项目中的库和 xamls 的引用:

xmlns:lib="clr-namespace:MyLibrary;assembly=MyLibrary"

MyLibrary 包含此 ImageButton - 一个简单的按钮扩展,主要用于保存图像。 WPF 看起来有点像:

<UserControl x:Class="MyLibrary.ImageButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:lib="clr-namespace:MyLibrary"
         x:Name="me"
         Width="auto"
         Height="22"
         HorizontalAlignment="Center" VerticalAlignment="Center">

<UserControl.Resources>
    <ResourceDictionary>
        <Style TargetType="{x:Type library:ImageButton}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
                    <Setter Property="Width" Value="22" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
                    <Setter Property="Width" Value="auto" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

<Button x:Name="_button" Click="Button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="auto">
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ElementName=me, Path=ImageSource}" Stretch="Uniform" />
        <TextBlock x:Name="_Text" Text="{Binding ElementName=me, Path=Text}" VerticalAlignment="Center" Margin="2, 0, 0, 0">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </StackPanel>
</Button>
</UserControl>

我猜 .cs 方面相当随意。

最后,我们有了 Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MyLibrary;component/Themes/MyTheme.xaml" />
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

“MyTheme”包含大量模板、颜色等。

【问题讨论】:

  • 您是否为您的程序集设置了 ThemeInfoAttribute? WPF 对于类库中的主题可能有点麻烦。
  • 可能是您的 ImageButton.ImageSource uri 被指向另一个程序集而感到困惑吗?而且由于您的网格没有最小尺寸,它可能在您的应用程序中。但对您不可见。尝试使用“pack”语法:msdn.microsoft.com/en-us/library/aa970069.aspx。此外,请与 Snoop 仔细检查该控件是否在您的可视化树中:snoopwpf.codeplex.com
  • ThemeInfo 确实设置好了。所有其他使用相同主题的用户控件都可以正常工作。只是 ImageButton 不会显示。 pack 语法也不会完成这项工作。 Snoop 确实找到了 ImageButton:基本 uri 是正确的,尽管 UserControls 内容(一个按钮,它再次包含一个图像)具有 NaN 的宽度/高度,并且没有背景/边框颜色。但是应该增长以适应图像集。有什么想法吗?

标签: c# wpf xaml user-controls resourcedictionary


【解决方案1】:

问题很可能源于您的类库项目不知道您想在任何地方使用资源字典即使您将资源字典合并到某个文件中。原因是 Visual Studio 和 Blend 都在寻找一个 ApplicationDefinition 文件供全局资源字典使用。

类库项目不能有 ApplicationDefinition 文件。但是,有一种方法可以共享资源,以便在设计时查看自定义控件项目。有关该主题的问题和答案可以在here 找到(免责声明:该链接当前指向我的答案)。

请注意,如果您使用上面链接的解决方案,您仍然必须在使用您的类库的任何应用程序项目的 App.xaml 级别引用您的资源字典,因为您的类库将不包含特定样式..

【讨论】:

    【解决方案2】:

    这个问题很老了,但这里有:

    经过大量比较后,我验证了让它工作的方法是在程序集级别的类库中的某处设置以下行:

    [assembly: ThemeInfo(
        ResourceDictionaryLocation.None, 
        ResourceDictionaryLocation.SourceAssembly 
    )]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      相关资源
      最近更新 更多