如何从一个资源文件中的图片插入一个WPF menuitem.icon [英] How to get an image from a resource file into an WPF menuitem.icon

查看:4309
本文介绍了如何从一个资源文件中的图片插入一个WPF menuitem.icon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的代码段(XAML C#):

I have the following piece of code (XAML C#):

        <Menu IsMainMenu="True" DockPanel.Dock="Top">
            <MenuItem Name="fileMenu" Header="_File" />
            <MenuItem Name="editMenu" Header="_Edit" />
            <MenuItem Name="setupMenu" Header="_Setup">
                <MenuItem Header="_Language">
                    <MenuItem.Icon> 
                         //I want to insert image here
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem Name="helpMenu" Header="_Help" />
        </Menu>

和含有称为lang.png图像的命名images.resx资源文件。
我怎样才能插入图像作为菜单项的图标?
有没有更好的办法?

And a resource file named images.resx containing an image called lang.png. How can I insert the image as an icon for the Menu-Item? Is there a better way?

推荐答案

随着杰森说,这是最好的图像作为资源添加到您的项目

As Jason said, it's better to add your images as Resources to your project.


  1. 打开属性为您的项目

  2. 选择垂直选项卡资源

  3. 选择从左边的组合框

  4. 图片选择添加资源 - >添加现有文件...从右边的组合框

  5. 找到您想使用,例如C1.png(它会自动被复制到资源文件夹在你的项目的根目录)

  6. 选择您的新特性的图像添加图像资源

  7. 在属性,设置生成操作为资源

  8. 打开设计器的.xaml文件,其中包含菜单和菜单项中添加图像.Icon然后将光标放在图像。

  1. Open "Properties" for your project
  2. Select Vertical-tab Resources
  3. Choose Images from the left ComboBox
  4. Choose "Add Resource -> Add Existing File..." from the right ComboBox
  5. Locate the Image you would like to use, e.g "C1.png" (it will automatically be copied to the Resources folder in the root of your project)
  6. Select properties on your newly added Resource Image
  7. In properties, set Build Action to Resource
  8. Open the designer for the .xaml file containing the Menu and add an Image in MenuItem.Icon and then place the cursor on Image.

XAML

<Menu IsMainMenu="True" DockPanel.Dock="Top"> 
    <MenuItem Name="fileMenu" Header="_File" /> 
    <MenuItem Name="editMenu" Header="_Edit" /> 
    <MenuItem Name="setupMenu" Header="_Setup"> 
        <MenuItem Header="_Language"> 
            <MenuItem.Icon>  
                 <Image/>
            </MenuItem.Icon> 
        </MenuItem> 
    </MenuItem> 
    <MenuItem Name="helpMenu" Header="_Help" /> 
</Menu> 



从性能,您现在可以选择在源物业和所有可用的图像资源符号会显示

From properties you can now select the symbol on the Source Property and all available Image resources will be displayed.

在此对话框中,你也可以选择添加,找到图像文件在磁盘上和所有上述步骤会为你由Visual Studio中进行。

From this dialog you can also choose "Add", locate an image file on the disk and all the above steps will be made for you by Visual Studio.

在XAML中Image.Source会是这个样子(这ofcourse也可以手动添加的结果URI )

The resulting uri for the Image.Source in xaml will look something like this (which ofcourse also can be added by hand)

<Menu IsMainMenu="True" DockPanel.Dock="Top">
    <MenuItem Name="fileMenu" Header="_File" />
    <MenuItem Name="editMenu" Header="_Edit" />
    <MenuItem Name="setupMenu" Header="_Setup">
        <MenuItem Header="_Language">
            <MenuItem.Icon>
                <Image Source="/MenuIconImage;component/Resources/C1.png" />
            </MenuItem.Icon>
        </MenuItem>
    </MenuItem>
    <MenuItem Name="helpMenu" Header="_Help" />
</Menu>

这篇关于如何从一个资源文件中的图片插入一个WPF menuitem.icon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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