C# wpf 图像资源和在运行时在 wpf 控件中更改图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/940592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:49:53  来源:igfitidea点击:

wpf image resources and changing image in wpf control at runtime

c#wpfimagedictionaryresources

提问by Tab

I would like to know exactly how to dynamically use a Dictionary Resource in the C# code behind - ie.. I would like to load images at runtime from an image resource within a dictionary

我想确切地知道如何在背后的 C# 代码中动态使用字典资源 - 即..我想在运行时从字典中的图像资源加载图像

I have a program that has 3 images in a WPF Dictionary - these are images set as image resources.

我有一个程序,在 WPF 字典中有 3 个图像 - 这些是设置为图像资源的图像。

Then in the code behind of my WPF Window I want to load any one of the three images based on user initiated events.

然后在我的 WPF 窗口后面的代码中,我想根据用户启动的事件加载三个图像中的任何一个。

There is no real code I have to show as nothing that I have done works.

没有我必须展示的真正代码,因为我所做的一切都不起作用。

Ideas?

想法?

采纳答案by Eric Smith

First, make sure you've defined your image resources like this:

首先,确保您已经像这样定义了图像资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ImageSource x:Key="image1">images/image1.jpg</ImageSource>
    <ImageSource x:Key="image2">images/image2.jpg</ImageSource>
</ResourceDictionary>

Secondly, I'm assuming that your WPF dictionary is in its own file. Now you have to make sure you've merged your dictionary into your main window's XAML (skip this step if your resource dictionary is defined inside of the window's XAML). In your window's XAML file, make sure you have something like this:

其次,我假设您的 WPF 字典在它自己的文件中。现在,您必须确保已将字典合并到主窗口的 XAML 中(如果您的资源字典是在窗口的 XAML 内定义的,则跳过此步骤)。在您窗口的 XAML 文件中,确保您有这样的内容:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="myDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Now, in your code-behind, you can use the FindResource() method to locate your image resource by it's key name (the value of the x:Key attribute on the ImageSource in the resource dictionary) like so:

现在,在您的代码隐藏中,您可以使用 FindResource() 方法通过键名(资源字典中 ImageSource 上的 x:Key 属性的值)来定位您的图像资源,如下所示:

imageControl.Source = (ImageSource)FindResource("image1");

Hope this helps!

希望这可以帮助!

回答by Raiford G. Bonnell

This is an addition to the accepted answer: When working within a ViewModelfrom MVVM, make sure to use the FindResourcefrom the view where the resource directory is added.

这是对已接受答案的补充:在ViewModel来自 MVVM 的环境中工作时,请确保使用FindResource来自添加资源目录的视图。

<Window x:Class="My.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModels="clr-namespace:My.ViewModels"
        Title="USA Hockey Player Evaluation tool" 
        Icon="/USAHockeyPlayerEval;component/View/Images/HET.ico"
        SizeToContent="WidthAndHeight"
        MinHeight="500px" MinWidth="800px">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Images.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Window.DataContext>
        <ViewModels:MainWindowMV/>
    </Window.DataContext>
    <StackPanel>
        <Menu>
            <MenuItem Header="File">
                <MenuItem Header="Save"></MenuItem>

My view in this case is a window (I know not correct MVVM ;-) )

在这种情况下,我的观点是一个窗口(我知道不正确的 MVVM ;-))

Image img = new Image();                                    
img.Source = (ImageSource)WindowReference.FindResource("Pluse"); 

Here the WindowReferenceis a reference to My.MainWindow.

这里WindowReference是对 的引用My.MainWindow