[英]External image in UserControl not loading when control is used
I have an Image
that I need to load from an external file into a Button
.我有一个需要从外部文件加载到Button
的Image
。
The Image
resides in a folder in the same directory as the final executable file. Image
与最终的可执行文件位于同一目录中的文件夹中。
/Resources/image.png
It's not included in the Project Resources
and it should not be for the needs of the Application
.它不包含在Project Resources
中,也不应该满足Application
的需要。
The Image
is loaded and displayed into a UserControl
fine, but when I place my UserControl
into my main View
, Image
已加载并显示到UserControl
中,但是当我将UserControl
放入主View
时,
I get the following error:我收到以下错误:
Cannot locate resource 'views/usercontrols/resources/playerbuttonsicons/repeat-icon.png'.
The UserControl
is located under Views/UserControls
in my Project Tree
as the error says.正如错误所说, UserControl
位于我的Project Tree
中的Views/UserControls
下。
I have tried various ways of specifying the image path (absolute, relative, uri
, pack
etc), but none of them worked.我尝试了各种指定图像路径的方法(绝对、相对、 uri
、 pack
等),但都没有奏效。
The problem is to be tackled using xaml
only, if possible.如果可能,只能使用xaml
解决该问题。
UserControl code:用户控制代码:
<UserControl x:Class="MusicPlayer.Views.UserControls.NowPlayingControl"
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:local="clr-namespace:MusicPlayer.Views.UserControls"
mc:Ignorable="d"
d:DesignWidth="400"
x:Name="ParentControl">
<!--Resources-->
<UserControl.Resources>
</UserControl.Resources>
<!--Design-->
<DockPanel x:Name="ParentContainer">
<Button x:Name="btnPlay">
<Button.Background>
<ImageBrush ImageSource="Resources/PlayerButtonsIcons/play-icon.png" />
</Button.Background>
</Button>
</DockPanel>
</UserControl>
MainView code:主视图代码:
<Window x:Class="MusicPlayer.MainView"
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:local="clr-namespace:MusicPlayer"
mc:Ignorable="d"
xmlns:vm="clr-namespace:MusicPlayer.ViewModels"
xmlns:uc="clr-namespace:MusicPlayer.Views.UserControls"
x:Name="ParentControl">
<!--Resources-->
<Window.Resources>
<vm:MainViewModel x:Key="VM" />
</Window.Resources>
<!--Design-->
<Grid x:Name="ParentContainer"
DataContext="{StaticResource VM}">
<uc:NowPlayingControl />
</Grid>
</Window>
I resolved my problem with the following steps.我通过以下步骤解决了我的问题。
Project Resources
.将所有外部文件包含在我的Project Resources
中。 Marked them as Content
and Copy Always
.将它们标记为Content
并Copy Always
。Images
into my UserControl
at run-time, I loaded them in my App.xaml
Resources
.我没有在运行时将Images
加载到我的UserControl
中,而是将它们加载到我的App.xaml
Resources
中。Images
are now used as StaticResource
in my UserControl
. Images
现在在我的UserControl
中用作StaticResource
。App.xaml App.xaml
<Application x:Class="MusicPlayer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MusicPlayer"
xmlns:ex="clr-namespace:SharpUtilities.WPF"
StartupUri="Views/MainView.xaml">
<Application.Resources>
<BitmapImage x:Key="PlayerPlayIcon"
UriSource="Resources/play-icon.png" />
</Application.Resources>
</Application>
UserControl.xaml UserControl.xaml
<UserControl x:Class="MusicPlayer.Views.UserControls.NowPlayingControl"
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:local="clr-namespace:MusicPlayer.Views.UserControls"
mc:Ignorable="d"
d:DesignWidth="400"
x:Name="ParentControl">
<DockPanel x:Name="ParentContainer">
<Button x:Name="btnPlay">
<Button.Background>
<ImageBrush ImageSource="{StaticResource PlayerPlayIcon}" />
</Button.Background>
</Button>
</DockPanel>
</UserControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.