简体   繁体   中英

xaml change image source

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

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).

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"

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?

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 . If you load the newly created image itself into the UI, does it retain its source?

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:

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)) .

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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