Wpf 当用户控件位于引用程序集中时,按钮上不显示图像
我有一个主WPF应用程序,它引用了我创建的WPF用户控件库。在库中,我有一个用户控件和一个用户控件使用的图像文件夹images。下面是我在用户控件中拥有的XAML的一部分:Wpf 当用户控件位于引用程序集中时,按钮上不显示图像,wpf,xaml,Wpf,Xaml,我有一个主WPF应用程序,它引用了我创建的WPF用户控件库。在库中,我有一个用户控件和一个用户控件使用的图像文件夹images。下面是我在用户控件中拥有的XAML的一部分: <UserControl x:Class="Alstom.UserControls.MainToolbar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schem
<UserControl x:Class="Alstom.UserControls.MainToolbar"
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:Alstom.UserControls"
mc:Ignorable="d"
d:DesignHeight="120" d:DesignWidth="3440">
<UserControl.Resources>
<Image x:Key="Subway" Source="/images/MainToolbar/Subway.png"/>
<Image x:Key="Globe" Source="/images/MainToolbar/Globe.png"/>
<Image x:Key="Gavel" Source="/images/MainToolbar/Gavel.png"/>
<Image x:Key="Clipboard" Source="/images/MainToolbar/Clipboard.png"/>
<Image x:Key="EllipsisVertical" Source="/images/MainToolbar/EllipsisVertical.png"/>
<Image x:Key="Check" Source="/images/MainToolbar/Check.png"/>
<Image x:Key="Bell" Source="/images/MainToolbar/Bell.png"/>
用户控件具有我编写的按钮,如下所示。注意,我将Content属性定义为StaticResource:
<Button x:Name="button" Grid.Row="1" Grid.Column="0" Content="{StaticResource Subway}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="48" Height="48"/>
在设计器中双击用户控件时,我会看到按钮上的图像
但我在解决方案中创建了一个主WPF应用程序,它引用外部程序集,如下所示:
<Window x:Class="MainToolBar.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:myMainToolbar="clr-namespace:Alstom.UserControls;assembly=Alstom.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="auto" Width="3440" ScrollViewer.VerticalScrollBarVisibility="Visible">
<Grid>
<myMainToolbar:MainToolbar HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
当我双击MainWindow.xaml时,我在设计器中的按钮上看不到图像,运行它时也看不到图像
我需要做什么才能在运行时显示图像?在用户控件项目中,映像有一个Build Action=Resource。但我试图将其中一个更改为嵌入式资源,但它仍然不起作用
我做错了什么?尝试将映像的生成操作设置为Resource并使用包URI:
<Image x:Key="Subway" Source="pack://application:,,,/YourAssemblyName;component/images/MainToolbar/Subway.png"/>
不要忘记将AssemblyName更改为assembly/WPF用户控件库项目的实际名称
WPF中的打包URI: