简体   繁体   English

使用控件时未加载 UserControl 中的外部图像

[英]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 .我有一个需要从外部文件加载到ButtonImage
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.我尝试了各种指定图像路径的方法(绝对、相对、 uripack等),但都没有奏效。
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.我通过以下步骤解决了我的问题。

  1. Included all the external files into my Project Resources .将所有外部文件包含在我的Project Resources中。 Marked them as Content and Copy Always .将它们标记为ContentCopy Always
  2. Instead of loading the Images into my UserControl at run-time, I loaded them in my App.xaml Resources .我没有在运行时将Images加载到我的UserControl中,而是将它们加载到我的App.xaml Resources中。
  3. The 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.

相关问题 在UserControl中使用Control.OnCreateControl时无法判断DesignMode - Control.OnCreateControl cannot judge DesignMode when used in a UserControl 图像未加载到图像控件中 - Image not loading in an Image Control 调整usercontrol大小时,包含图像图像的WPF userControl不会调整大小 - WPF userControl containing an image image doesnt resize when usercontrol is resized WPF UserControl加载时抛出NullReferenceException - WPF UserControl throws NullReferenceException when loading 将用户控件添加到父控件时,将其识别 - Aware UserControl when add it to a parent control 仅在可见时延迟加载System.Windows.Control.Image - Lazy loading System.Windows.Control.Image only when visible 使用web.config控件引用以编程方式加载UserControl - Loading UserControl programmatically using web.config control references 将大图像加载到图像控件中 - Loading large images into image control 如何获得对用户控件中存在的表Layoutpanel的每个单元格的控制,以及如何在另一个winform中使用该用户控件? - How to get control of each cell of a table Layoutpanel present in a usercontrol and that user control is used in another winform? 加载外部dll时ReflectionTypeLoadException - ReflectionTypeLoadException when loading external dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM