简体   繁体   English

xaml更改图像源

[英]xaml change image source

In c# I am making an application using the xaml designer. 在C#中,我正在使用xaml设计器制作应用程序。

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) 问题:如果我将图像源更改为另一个图像源,则没有出现:提示,如果已经定义了图像,则将其更改为“ NULL”(无图像)

I have used a method (which I found on the web) to change an imagesource (string) into an image(object). 我使用一种方法(我在网上找到了)将图像源(字符串)更改为图像(对象)。

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. 我已经尝试过多次更改某些xaml图片的图像源的方法,只是将xaml中的图像源更改为另一个图像(我将其放入项目中),但是不幸的是,当我这样做时该图像不可见。

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? 你们中的任何人都知道可能是什么问题吗?

I had a similar problem, and this worked for me: 我有一个类似的问题,这对我有用:

            var imageSource = new BitmapImage();
            imageSource.BeginInit();
            imageSource.StreamSource = memoryStream;
            imageSource.CacheOption = BitmapCacheOption.OnLoad;
            imageSource.EndInit();

Have you tried binding the source in XAML to a URI and then updating that? 您是否尝试过将XAML中的源绑定到URI,然后进行更新?

Like this: 像这样:

<Image Source="{Binding ImageUri}" />

Then having a property somewhere in your datacontext which would be like: 然后在您的数据上下文中的某处具有一个属性,如下所示:

public class myClass: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Uri imageUri;

    public Uri ImageUri
    {
        get
        {
            return imageUri;
        }
        set
        {
           imageUri = value;
           if(PropertyChanged != null)
           {
               PropertyChanged(this, new PropertyChangedEventArgs("ImageUri"));
           }
        }
    }
}

Have you verified that your method actually succeeds and returns the correct image source? 您是否已验证您的方法确实成功并返回了正确的图像源? If it does there should be no problem reassigning Source . 如果这样做,重新分配Source应该没有问题。 If you load the newly created image itself into the UI, does it retain its source? 如果您将新创建的图像本身加载到UI中,它是否保留其源?

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

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: 如果您有一个可以用作XAML的字符串,则可以直接调用XAML解析器用来构建图像源的转换器:

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

string stringValue = ...

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

The converter instance (an ImageSourceConverter in this case) can also be retrieved dynamically by System.ComponentModel.TypeDescriptor.GetConverter(typeof(TypeToConvertTo)) . 也可以通过System.ComponentModel.TypeDescriptor.GetConverter(typeof(TypeToConvertTo))动态检索转换器实例(在这种情况下为ImageSourceConverter System.ComponentModel.TypeDescriptor.GetConverter(typeof(TypeToConvertTo))

This conversion will also be done automatically if you use data-binding as in TylerD87's answer . 如果您像TylerD87的答案中那样使用数据绑定,此转换也将自动完成。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM