【发布时间】:2017-06-02 18:38:52
【问题描述】:
我正在尝试将 XAML 页面上椭圆内的 imageSource 绑定到 ViewModel 中的 ImageSource 属性,因为我在项目中使用 MVVM 方法。我可以通过断点确认 c# 中的属性获取图像并填充,但由于某种奇怪的原因,它没有显示在 XAML 中,当我在混合中使用 livepropertyExplorer 分析源属性时,ImageSource 中的源显示“0 ”。这是我的代码。
XAML
<Ellipse x:Name="ProfileImage" Style="{StaticResource ProfilePicStyle}">
<Ellipse.Fill>
<ImageBrush ImageSource="{x:Bind ViewModel.Banner, Mode=OneWay}"
Stretch="UniformToFill"/>
</Ellipse.Fill>
</Ellipse>
视图模型
public AllVideosViewModel() : base()
{
//var bit = new BitmapImage();
//bit.UriSource = (new Uri("ms-appx:///Assets/Storelogo.png"));
//Banner = bit;
//Above 3 lines were test code and it works perfect and binding works in this case, but I want the binding as coded in the Initialize method below, because I want that image to bind with the thumbnails of the collection.
Initialize();
}
private async void Initialize()
{
var VideoFiles = (await KnownFolders.VideosLibrary.GetFilesAsync(CommonFileQuery.OrderByName)) as IEnumerable<StorageFile>;
foreach (var file in VideoFiles)
{
<...some code to fill grid view on the same XAML page which works perfect...>
Banner = await FileHelper.GetDisplay(file);//this method returns Image just fine because I am using the same method to display thumbnails on the same XAML page which work fine.
}
}
提前致谢。
更新示例项目
【问题讨论】:
-
“此方法返回图像 ...”。实际上,它应该返回一个 ImageSource(或像 BitmapImage 这样的派生类型)。 Banner 属性的类型是什么?
-
banner属性类型为ImageSource,GetDisplay方法返回BitmapImage
-
方法很好,因为我指定我使用相同的方法在同一页面上用缩略图填充我的 gridview
-
改成本地图片,行吗?
-
是的,正如我在代码中提到的,如果我将它绑定到资产文件夹中的图像,那么它可以正常工作@Justin XL
标签: c# image xaml binding uwp-xaml