locked
My images aren't showing anymore RRS feed

  • Question

  • So I made a WPF .NET Framework app, then started a new .NET Core WPF project, copied over all the files etc and the program works as a .NET Core app except the images don't show anymore. The images show in a viewbox that is on it's own window, here is the XAML for that:

    <Viewbox x:Name="MyViewBox">
        <Image x:Name="MyImage" Source="{Binding MyImageSource}"/>
    </Viewbox>
    

    Here is the property it is bound to:

    private string _myImageSource = "../Resources/Images/MyImage.png";
    public string MyImageSource
    {
        get { return _myImageSource; }
        set 
        { 
            _myImageSource = value;
            OnPropertyChanged();
        }
    }
    

    The string that points to the image file is generated based on user choice elsewhere, the above is the default. This all worked totally fine as a .NET Framework WPF app. Why doesn't it work as a .NET Core app?

    Apologies if more code is needed to identify the problem, let me know and I'll post it.

    Wednesday, March 4, 2020 4:27 PM

Answers

  • Hi,

    You can try to add a converter:

     <Window.Resources>
            <local:StringToImageSourceConverter x:Key="stringtoimage"/>
        </Window.Resources>
        <Grid>
            <Image Source="{Binding MyImageSource,Converter={StaticResource stringtoimage}}"/>
        </Grid>
      public class StringToImageSourceConverter : IValueConverter
        {
       
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string path = (string)value;
                if (!string.IsNullOrEmpty(path))
                {
                    return new BitmapImage(new Uri(path, UriKind.Absolute));
                }
                else
                {
                    return null;
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
    
            }
    
        }

    Best Regards,

    Alex


    "Windows Presentation Foundation" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    • Marked as answer by Integrater Friday, March 6, 2020 10:16 AM
    Thursday, March 5, 2020 6:06 AM