我正在为DataTemplate创建一个自定义UserControl,它接受元数据对象并将FilePath分配给图像(这些路径由用户在运行时定义)。但是,尽管没有抛出任何错误,并确保图像的来源是正确的,并导致生成实际的文件,但图像在运行时不会出现。我在图像周围加了一个边框,它就会显示出来,但图像没有。
以下是UserControl的XAML代码:
<Border x:Name="Outline"
BorderThickness="1"
BorderBrush="Red">
<Grid>
<Image x:Name="DisplayImage"/>
<MediaElement x:Name="DisplayVideo"/>
</Grid>
</Border>
以及设置图像源的代码:
public MediaContainer() {
InitializeComponent();
Loaded += (_, _) => Load();
}
private void Load() {
Metadata data = (Metadata)DataContext;
(double height, double width) = Scale(data); // This never returns 0, for either the height or the width
Outline.Width = width + (Configs.OUTLINE_WIDTH * 2);
Outline.Height = height + (Configs.OUTLINE_WIDTH * 2);
Uri uri = new(data.FilePath, UriKind.RelativeOrAbsolute);
BitmapImage source = new(uri);
DisplayImage = new() {
Source = data.FileType == FileType.Gif ? null : source,
Height = height,
Width = width
};
static (double, double) Scale(Metadata meta) {
double _height = meta.Height <= 0 ? Configs.HEIGHT : meta.Height;
double _width = meta.Width <= 0 ? Configs.HEIGHT : meta.Width;
if (meta.Height != Configs.HEIGHT) {
double scale = Configs.HEIGHT / meta.Height;
_height = Configs.HEIGHT;
_width = meta.Width * scale;
}
return new(_height, _width);
}
}
我曾尝试手动设置图像和媒体元素的z索引,但没有成功。那么,是什么阻止了它的显示呢?