gpt4 book ai didi

.net - WPF 图像 UriSource、数据绑定(bind)和缓存

转载 作者:行者123 更新时间:2023-12-02 05:08:46 31 4
gpt4 key购买 nike

我目前正在为数据绑定(bind) WPF 图像工作:

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

足够简单。

现在,向该图像添加缓存(我希望能够在加载本地文件后对其进行操作/删除)。我发现您可以将 CacheOption="OnLoad"添加到 .

中的标记
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ThumbFile, Converter={StaticResource myConverter2}}" />
</Image.Source>
</Image>

然后我必须有一个转换器来将本地文件转换为 BitmapImage。

<local:LocalUriToImageConverter x:Key="myConverter2"/>

public class LocalUriToImageConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return null;
}

if (value is string)
{
value = new Uri((string)value);
}

if (value is Uri)
{
System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
bi.BeginInit();
//bi.DecodePixelWidth = 80;
bi.DecodePixelHeight = 60;
bi.UriSource = (Uri)value;
bi.EndInit();
return bi;
}

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}

出于某种原因,这甚至没有开始工作。没有错误,但控件似乎没有被绑定(bind)。 ThumbFile 属性的获取和转换器中的断点都没有到达,即使该控件创建了许多实例。切换回其他图像源标签工作正常。

最佳答案

我无法从您的代码中得知发生了什么,但我会使用 Snoop深入研究它,看看发生了什么。您应该能够看到任何绑定(bind)错误并查看 Image 上的 DataContext 是什么,并确保 DataContext 上的 ThumbFile 属性具有什么你会期望。

关于.net - WPF 图像 UriSource、数据绑定(bind)和缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8086076/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com