C# “如何显示默认值”;“图像”;如果加载的图像没有';不存在?
我正在制作一个应用程序,用户可以通过某个路径加载图像。然后,存储加载的图像的路径,下次用户想要再次打开文档时,将根据该路径加载图像 但是,我想处理的是,如果前一个路径无效(图像移动/删除),那么这不是一个错误,但是,这种对象显示: 我想知道是否有任何方法可以使应用程序在没有该图像的情况下做到这一点。我的意思是,也许C#对这种“缺失”的图像有某种属性 多谢各位C# “如何显示默认值”;“图像”;如果加载的图像没有';不存在?,c#,wpf,image,io,C#,Wpf,Image,Io,我正在制作一个应用程序,用户可以通过某个路径加载图像。然后,存储加载的图像的路径,下次用户想要再次打开文档时,将根据该路径加载图像 但是,我想处理的是,如果前一个路径无效(图像移动/删除),那么这不是一个错误,但是,这种对象显示: 我想知道是否有任何方法可以使应用程序在没有该图像的情况下做到这一点。我的意思是,也许C#对这种“缺失”的图像有某种属性 多谢各位 另外,上面的“缺失”图像实际上是一个“缺失”图像。您可以使用图像控件的ImageFailed事件 如果引发该事件,请在此处设置错误映像
另外,上面的“缺失”图像实际上是一个“缺失”图像。您可以使用图像控件的ImageFailed事件 如果引发该事件,请在此处设置错误映像
<Image src= ImageFailed="OnImageFailed" />
您可以使用图像控件的ImageFailed事件 如果引发该事件,请在此处设置错误映像
<Image src= ImageFailed="OnImageFailed" />
您可以创建一个自定义的
转换器
来为您执行此操作。为您添加一个属性,以便能够设置默认的图像
路径:
[ValueConversion(typeof(string), typeof(ImageSource))]
public class EmptyImageToImageSourceConverter : IValueConverter
{
/// <summary>
/// Converts an empty string value into the DefaultImagePath property value if it exists, or a DependencyProperty.UnsetValue otherwise.
/// </summary>
public string DefaultImagePath { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || targetType != typeof(ImageSource)) return DependencyProperty.UnsetValue;
string imagePath = value.ToString();
return imagePath.IsNullOrEmpty() ? DefaultImagePath.IsNullOrEmpty() ? DependencyProperty.UnsetValue : DefaultImagePath : imagePath;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
[ValueConversion(typeof(string)、typeof(ImageSource))]
公共类EmptyImageToImageSourceConverter:IValueConverter
{
///
///如果存在空字符串值,则将其转换为DefaultImagePath属性值,否则将其转换为DependencyProperty.UnsetValue。
///
公共字符串DefaultImagePath{get;set;}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(value==null | | targetType!=typeof(ImageSource))返回dependencProperty.unset值;
字符串imagePath=value.ToString();
返回imagePath.IsNullOrEmpty()?DefaultImagePath.IsNullOrEmpty()?DependencyProperty.UnsetValue:DefaultImagePath:imagePath;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回dependencProperty.unset值;
}
}
然后你可以这样使用它:
<Converters:EmptyImageToImageSourceConverter x:Key="EmptyImageToImageSourceConverter"
DefaultImagePath="pack://application:,,,/AppName;component/Images/DefaultImage.png" />
请注意,此
转换器
与上面的字符串
文件路径类似,而不是位图图像
或图像源
对象。它还需要您提供一个默认的图像来显示。您可以创建一个自定义的转换器来为您执行此操作。为您添加一个属性,以便能够设置默认的图像
路径:
[ValueConversion(typeof(string), typeof(ImageSource))]
public class EmptyImageToImageSourceConverter : IValueConverter
{
/// <summary>
/// Converts an empty string value into the DefaultImagePath property value if it exists, or a DependencyProperty.UnsetValue otherwise.
/// </summary>
public string DefaultImagePath { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || targetType != typeof(ImageSource)) return DependencyProperty.UnsetValue;
string imagePath = value.ToString();
return imagePath.IsNullOrEmpty() ? DefaultImagePath.IsNullOrEmpty() ? DependencyProperty.UnsetValue : DefaultImagePath : imagePath;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
[ValueConversion(typeof(string)、typeof(ImageSource))]
公共类EmptyImageToImageSourceConverter:IValueConverter
{
///
///如果存在空字符串值,则将其转换为DefaultImagePath属性值,否则将其转换为DependencyProperty.UnsetValue。
///
公共字符串DefaultImagePath{get;set;}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(value==null | | targetType!=typeof(ImageSource))返回dependencProperty.unset值;
字符串imagePath=value.ToString();
返回imagePath.IsNullOrEmpty()?DefaultImagePath.IsNullOrEmpty()?DependencyProperty.UnsetValue:DefaultImagePath:imagePath;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回dependencProperty.unset值;
}
}
然后你可以这样使用它:
<Converters:EmptyImageToImageSourceConverter x:Key="EmptyImageToImageSourceConverter"
DefaultImagePath="pack://application:,,,/AppName;component/Images/DefaultImage.png" />
请注意,此转换器
与上面的字符串
文件路径类似,而不是位图图像
或图像源
对象。它还要求您提供要显示的默认图像