C# uwp图像绑定不适用于x:Bind
我试图将XAML页面上椭圆内的imageSource绑定到ViewModel中的imageSource属性,因为我在项目中使用MVVM方法。我可以通过断点确认c#中的属性获取图像并填充,但由于某些奇怪的原因,它没有显示在XAML中,当我在混合中使用livepropertyExplorer分析源属性时,ImageSource中的源显示为“0”。这是我的密码 XAMLC# uwp图像绑定不适用于x:Bind,c#,image,xaml,binding,uwp-xaml,C#,Image,Xaml,Binding,Uwp Xaml,我试图将XAML页面上椭圆内的imageSource绑定到ViewModel中的imageSource属性,因为我在项目中使用MVVM方法。我可以通过断点确认c#中的属性获取图像并填充,但由于某些奇怪的原因,它没有显示在XAML中,当我在混合中使用livepropertyExplorer分析源属性时,ImageSource中的源显示为“0”。这是我的密码 XAML <Ellipse x:Name="ProfileImage" Style="{StaticResource ProfilePi
<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.
}
}
public AllVideosViewModel():base()
{
//var bit=新的BitmapImage();
//bit.UriSource=(新Uri(“ms”)-appx:///Assets/Storelogo.png"));
//横幅=位;
//上面的3行是测试代码,在本例中,它工作得很完美,绑定也工作得很好,但我希望绑定与下面的初始化方法中的代码相同,因为我希望该图像与集合的缩略图绑定。
初始化();
}
私有异步void Initialize()
{
var VideoFiles=(wait KnownFolders.VideosLibrary.getfileasync(CommonFileQuery.OrderByName))作为IEnumerable;
foreach(视频文件中的var文件)
{
Banner=wait FileHelper.GetDisplay(file);//此方法返回的图像很好,因为我使用相同的方法在相同的XAML页面上显示缩略图,效果很好。
}
}
提前感谢。
更新示例项目
我在您的代码中注意到了一些事情
MainViewModel
需要实现INotifyPropertyChanged
,您的Banner
属性需要引发属性更改事件。这就是为什么在UI上看不到任何图像wait bit.SetSourceAsync()
而不是bit.SetSourceAsync()
,因为它不会阻塞UI线程Image
控件,而是直接使用ImageBrush
,因此您应该将解码大小设置为所需的大小,以免浪费内存。注意,图像
控件会自动为您执行此操作
bit = new BitmapImage
{
DecodePixelWidth = 48,
DecodePixelHeight = 48
};
希望这有帮助 “此方法返回图像…”。实际上,它应该返回一个ImageSource(或者像BitmapImage这样的派生类型)。Banner属性的类型是什么?Banner属性的类型是ImageSource,方法GetDisplay返回BitmapImagemethod很好,因为我指定使用相同的方法在同一页面上用缩略图填充gridview将其更改为本地图像,它可以工作吗?是的,正如我在代码中提到的,如果我将它绑定到资产文件夹中的图像,那么它可以正常工作@Justin xl为什么我需要提供Inotify属性?x:Bind不是应该自动执行此操作的一种方法吗?我的意思是如果不是的话,那么X的函数是什么:用“单向”模式绑定?不。。。x:Bind只提供编译时绑定,它比传统绑定提供更好的性能。其他一切都没有改变。x:Bind to function with oneway是周年更新中的新功能。在函数绑定中用作参数时,您仍然需要INOTIFYPROPERTYCHANGE的属性,否则它如何知道何时重新计算?在这种情况下,我直接绑定到viewmodel中的属性,是的,您的解决方案与实现的Inotify属性一起工作,但我仍然不清楚单向函数是什么,如果我们仍然需要实现Inotify,是的,我的项目的最低目标是Anniversay Update我正在更改函数中特定属性的值,这就是我需要Inotify的原因吗?