具有自定义控件的 WPF 类库 - 控件不会显示
本文关键字:控件 显示 类库 自定义控件 WPF | 更新日期: 2023-09-27 18:35:13
在网上搜索了几个小时后,我转向大家:
我的 WPF 类库(.NET 3.5,COM 可见)中有一个窗体,它使用用户控件和主题文件。
问题:使用 WPF 应用程序时,按钮工作正常,但在此 .NET 3.5 COM 可见类库中,它们根本不会显示。可以使用和工作库中的其他对象。可能是什么问题?我倾向于找不到的资源字典,或者一些找不到的资源,但我无法将手指放在上面。任何帮助将非常受欢迎!
一些代码
资源通过
<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>
不要忘记项目中库和 xaml 的引用:
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>
其中"我的主题"包含大量模板,颜色等。
此问题很可能源于这样一个事实,即您的类库项目不知道您希望在任何地方使用资源字典,即使您将资源字典合并到某个文件中也是如此。这样做的原因是Visual Studio和Blend都在寻找一个应用程序定义文件供全局资源字典使用。
类库项目不能具有应用程序定义文件。但是,有一种方法可以共享资源,以便在设计时查看自定义控件项目。关于该主题的问题和答案可以在这里找到(免责声明:链接目前指向我的答案)。
请注意,如果使用上面链接的解决方案,您仍然必须在使用类库的任何应用程序项目的 App.xaml 级别引用资源字典,因为类库将不包含特定的样式。
好吧,
这个问题相当古老,但这里是:
在进行了大量比较之后,我验证了使其工作的方法是在程序集级别的类库中的某个位置设置以下行:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly
)]