当用户控件在引用程序集中时,图像不会出现在按钮上 [英] Images not appearing on buttons when user control is in referenced assembly

查看:22
本文介绍了当用户控件在引用程序集中时,图像不会出现在按钮上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主 WPF 应用程序,它引用了我创建的 WPF 用户控件库.在库中,我有一个用户控件和一个图像文件夹(用户控件使用的图像).这是我在用户控件中拥有的 XAML 的一部分:

I have a main WPF application which references a WPF User Control Library that I created. In the library, I have one user control and an images folder (images used by user control). Here is part of the XAML that I have in the user control:

<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:

The user control has buttons which I wrote like the following (note that I defined the Content property as StaticResource:

<Button x:Name="button" Grid.Row="1" Grid.Column="0" Content="{StaticResource Subway}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="48" Height="48"/>

当我双击用户控件时,在设计器中,我看到按钮上的图像.

When I double click the user control, in the designer, I see the images on the buttons.

但是我在我的解决方案中创建了一个主要的 WPF 应用程序,它像这样引用外部程序集:

But I created a main WPF application in my solution which references the external assembly like so:

<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 时,我在设计器中看不到按钮上的图像,运行它时也看不到.

When I double-click the MainWindow.xaml, I don't see the images on the button in the designer, nor when I run it.

我需要做什么才能在运行时显示图像?在用户控件项目中,图像具有 Build Action = Resource.但是我尝试将其中一个更改为Embedded Resource,它仍然不起作用.

What do I need to do to show the images at runtime? In the user control project, the images have a Build Action = Resource. But I tried to change one of them to Embedded Resource, and it still did not work.

我做错了什么?

推荐答案

尝试将图像的构建操作设置为 Resource 并使用包 URI:

Try to set the build action of the images to Resource and use a pack URI:

<Image x:Key="Subway" Source="pack://application:,,,/YourAssemblyName;component/images/MainToolbar/Subway.png"/>

不要忘记将YourAssemblyName"更改为程序集/WPF 用户控件库项目的实际名称.

Don't forget to change "YourAssemblyName" to the actual name of your assembly/WPF User Control Library project.

在 WPF 中打包 URI: https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx

这篇关于当用户控件在引用程序集中时,图像不会出现在按钮上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆