问题描述
我制作了一个 WPF UserControl
并想在另一个项目中引用它。
但即使 UserControl
在其自己的程序集中可用,但在其他项目中引用时,会出现 StaticResource
not found 问题。
我的 UserControl
看起来像这样:
<UserControl x:Class="DiagramDesigner.VacSymGrmV"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DiagramDesigner"
xmlns:s="clr-namespace:DiagramDesigner"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<local:DesignerCanvas Focusable="true" x:Name="vacgrm"
Background="{StaticResource WindowBackgroundBrush}"
Margin="10" FocusVisualStyle="{x:Null}"
ContextMenu="{StaticResource DesignerItemContextMenu}"/>
</Grid>
</UserControl>
问题点是:{Static Resource WindowBackgroundBrush}
,它是定义在XAML文件中的资源:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="disabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="disabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#000" />
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
</ResourceDictionary>
这个程序集是一个 EXE,在 UserControl
的设计窗口中,我可以看到它正确显示,并且可以编译和运行。
但是当我想在另一个 WPF 应用程序中使用 UserControl
时,就像下面的代码一样,这个应用程序在启动时抛出一个异常,声称没有找到 WindowBackgroundBrush
:
<Window x:Class="XVacSysComp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vac="clr-namespace:DiagramDesigner;assembly=VacSymDgm"
xmlns:local="clr-namespace:XVacSysComp;assembly=XVacSysComp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<vac:VacSymGrmV>
</vac:VacSymGrmV>
</Grid>
</Window>
VacSymGrmV
是 UserControl
并且毫不奇怪,UserControl
未正确显示在设计窗口中。
异常说:
异常:找不到'WindowBackgroundBrush',注意大写/小写...
在另一个程序集中使用静态资源时,这是否与静态资源的位置有关?
解决方法
异常告诉您确切的问题,未找到 WindowBackgroundBrush
。
找不到'WindowBackgroundBrush',注意大写/小写...
资源定义在您在包含程序集 (EXE) 中的任何位置合并的资源字典中,例如应用程序资源。如果您在那里使用用户控件,则可以解析静态资源。
但是,如果您使用来自不同应用程序的用户控件,该应用程序将如何自动访问来自任何其他程序集的资源?即使您的用户控件位于原始应用程序的程序集 (EXE) 中,仅从不同的应用程序或库中引用它也不会引导其他 WPF 应用程序或神奇地将引用的资源从那里添加到执行程序集。>
解决方法很简单,将对应的资源字典合并到其他应用的应用资源字典中或者是范围内不同的资源字典来解析资源。如果是共享资源,请考虑创建一个可供不同项目引用的专用程序集。
<ResourceDictionary>
<!-- ...other resources. -->
<ResourceDictionary.MergedDictionaries>
<!-- ...other resource dictionaries. -->
<ResourceDictionary Source="pack://application:,/MySharedAssembly;component/MySharedResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
有关引用 resource dictionary from a referenced assembly 和示例中使用的源语法的更多信息,请查看 pack URI documentation。
根据您的情况,您还可以将资源与您的用户控制合并并提供,但我想这不适用于有问题的资源,因为它是应用程序背景。
,您应该将资源字典导入到您的 UserControl
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.picture_to_load);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);return getImageWebResource(bis);