xaml更改图像源 [英] xaml change image source

查看:76
本文介绍了xaml更改图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我正在使用xaml设计器制作应用程序.

In c# I am making an application using the xaml designer.

问题:如果我将图像源更改为另一个图像源,则没有出现:提示,如果已经定义了图像,则将其更改为"NULL"(无图像)

PROBLEM: IF I CHANGE THE IMAGE SOURCE INTO ANOTHER IMAGE SOURCE, NOTHING APPEARS: THOUGH, IF THE IMAGE WAS ALREADY DEFINED, IT CHANGED INTO "NULL" (NO IMAGE)

我已经使用一种方法(我在网络上找到了)将图像源(字符串)更改为图像(对象).

I have used a method (which I found on the web) to change an imagesource (string) into an image(object).

我已经尝试过多次更改某些xaml图片的图像源的方法,只是将xaml中的图像源更改为另一个图像(放置在项目中),但是不幸的是,当我这样做时该图像不可见.

I've tried multiple times to change the image source of some xaml pictures by just changes the imagesource in xaml into another image (that I put in the project) but unfortunately the image is not visible when I do it this way.

我使用的图像和方法如下:

The image and method I used are the following ones:

ms-appx:///Assets/bank_employee.jpg"

ms-appx:///Assets/bank_employee.jpg"

imageFromXaml.Source = imgCreatedFromMethod.Source;

private static Image srcToImage(string source)   
{   
        Uri imageUri = new Uri(source);    
        BitmapImage imageBitmap = new BitmapImage(imageUri);   
        Image img = new Image();   
        img.Source = imageBitmap;    
        return img;
}

你们中的任何人都知道可能是什么问题吗?

Does anyone of you know what the problem could be?

推荐答案

您是否已验证您的方法确实成功并返回了正确的图像源?如果这样,重新分配Source应该没有问题.如果您将新创建的图像本身加载到UI中,它会保留其源代码吗?

Have you verified that your method actually succeeds and returns the correct image source? If it does there should be no problem reassigning Source. If you load the newly created image itself into the UI, does it retain its source?

this.Content = imgCreatedFromMethod;  // where "this" is the window

顺便说一句,没有必要实现自己的转换功能.如果您有一个可以用作XAML的字符串,则可以直接调用XAML解析器用来构建图像源的转换器:

By the way, it would not be necessary to implement your own conversion function. If you have a string that would be valid as XAML, you can directly call the converter that the XAML parser uses to build an image source:

using System.Globalization;
using System.Windows.Media;

string stringValue = ...

ImageSourceConverter converter = new ImageSourceConverter();
ImageSource imageSource = converter.ConvertFrom(
    null, CultureInfo.CurrentUICulture, stringValue);

System.ComponentModel.TypeDescriptor.GetConverter(typeof(TypeToConvertTo))也可以动态检索转换器实例(在这种情况下为ImageSourceConverter).

The converter instance (an ImageSourceConverter in this case) can also be retrieved dynamically by System.ComponentModel.TypeDescriptor.GetConverter(typeof(TypeToConvertTo)).

如果您使用数据绑定(如 TylerD87的答案),此转换也将自动完成.您还可以查看触发器并以这种样式定义两个图像:

This conversion will also be done automatically if you use data-binding as in TylerD87's answer. You can also look into triggers and define both images in the style like this:

<Image>
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Source" Value="original path" />
            <Style.Triggers>
                <Trigger ...>
                    <Setter Property="Source" Value="new path" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

这篇关于xaml更改图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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