【发布时间】: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