WPF MaterialDesign跨解决方案自定义UserControl图标不显示问题
问题描述
我有两个独立解决方案:
- 解决方案A:定义了一个自定义UserControl,相关代码如下:
XAML代码:
C#依赖属性代码:<TextBox BorderBrush="{Binding ValueFG, RelativeSource={RelativeSource AncestorType=UserControl}}" Foreground="{Binding ValueFG, RelativeSource={RelativeSource AncestorType=UserControl}}" HorizontalAlignment="Right" md:HintAssist.HelperText="{Binding ValueHT, RelativeSource={RelativeSource AncestorType=UserControl}}" md:TextFieldAssist.LeadingIcon="{Binding ValueIcon, RelativeSource={RelativeSource AncestorType=UserControl}}" Text="{Binding ValueInput, RelativeSource={RelativeSource AncestorType=UserControl}, UpdateSourceTrigger=PropertyChanged}" Width="{Binding ValueWidth, RelativeSource={RelativeSource AncestorType=UserControl}}"/>public string ValueIcon { get { return (string)GetValue(ValueIconProperty); } set { SetValue(ValueIconProperty, value); } } public static readonly DependencyProperty ValueIconProperty = DependencyProperty.Register( nameof(ValueIcon), typeof(string), typeof(TxtInput), new PropertyMetadata("HumanGreeting")); - 解决方案B:已添加命名空间并引用解决方案A,使用该UserControl时尝试通过属性设置图标:
<uc:TxtInput KeyText="Developer Name" DockPanelMargin="{Binding NameMargin}" ValueFG="{Binding NameFG}" ValueHT="{Binding NameHT}" ValueInput="{Binding Name}" ValueIcon="HumanGreetingVariant" Style="{StaticResource TxtInputLocal}"/>
问题:MaterialDesign图标完全无法显示,无论是指定图标还是使用默认值都无效。但在同一解决方案内使用该UserControl、其他项目以字符串形式设置图标时均正常工作。

解决思路与方案
这种跨解决方案的图标不显示问题,核心原因多为资源引用缺失、类型转换异常或版本不兼容,以下是具体解决步骤:
1. 确保解决方案B引入MaterialDesign完整资源
在解决方案B的App.xaml中添加MaterialDesign资源字典引用(若未添加):
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Icons.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignIcons.Wpf;component/Themes/MaterialDesignIcons.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
同时确认解决方案B已通过NuGet安装MaterialDesignThemes和MaterialDesignIcons包,版本需与解决方案A完全一致。
2. 修改依赖属性类型为PackIconKind枚举
MaterialDesign的TextFieldAssist.LeadingIcon本质接受PackIconKind枚举类型,跨解决方案时字符串到枚举的自动转换可能失效。修改解决方案A的代码:
using MaterialDesignThemes.Wpf; public PackIconKind ValueIcon { get { return (PackIconKind)GetValue(ValueIconProperty); } set { SetValue(ValueIconProperty, value); } } public static readonly DependencyProperty ValueIconProperty = DependencyProperty.Register( nameof(ValueIcon), typeof(PackIconKind), typeof(TxtInput), new PropertyMetadata(PackIconKind.HumanGreeting));
XAML绑定无需修改,解决方案B中使用时直接指定枚举值:
<uc:TxtInput KeyText="Developer Name" DockPanelMargin="{Binding NameMargin}" ValueFG="{Binding NameFG}" ValueHT="{Binding NameHT}" ValueInput="{Binding Name}" ValueIcon="{x:Static md:PackIconKind.HumanGreetingVariant}" Style="{StaticResource TxtInputLocal}"/>
3. 验证资源加载有效性
在解决方案B的任意窗口/UserControl中添加测试图标,确认资源是否正常加载:
<md:PackIcon Kind="HumanGreetingVariant" Width="24" Height="24"/>
若该图标能显示,说明资源无问题,需排查UserControl的绑定逻辑;若仍无法显示,检查NuGet包安装状态、资源路径是否正确。
4. 确认程序集引用一致性
确保解决方案B引用的解决方案A程序集为最新版本,且两个解决方案的MaterialDesign相关NuGet包版本完全匹配,版本差异可能导致资源无法共享。
内容的提问来源于stack exchange,提问作者06luki06
相关产品推荐
相关产品推荐

