Cannot delete file used by some other process

前端 未结 2 527
盖世英雄少女心
盖世英雄少女心 2020-11-27 08:40

I am displaying some image in my wpf app using following code:

 

        
2条回答
  •  难免孤独
    2020-11-27 09:09

    According to Clemens suggestion, here is the binding converter to have a good code-reuse:

    namespace Controls
    {
        [ValueConversion(typeof(String), typeof(ImageSource))]
        public class StringToImageSourceConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (!(value is string valueString))
                {
                    return null;
                }
                try
                {
                    ImageSource image = BitmapFrame.Create(new Uri(valueString), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                    return image;
                }
                catch { return null; }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    And there is a string for binding, for example

    public string MyImageString { get; set; } = @"C:\test.jpg"
    

    And in the UI the converter is used, in my case from the Library named "Controls"

    
        
            
        
        
            
        
    
    

提交回复
热议问题