Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#UWP更改ListView绑定中的图像源_C#_Image_Listview_Uwp - Fatal编程技术网

C#UWP更改ListView绑定中的图像源

C#UWP更改ListView绑定中的图像源,c#,image,listview,uwp,C#,Image,Listview,Uwp,我为自己提供了一个C#UWP解决方案,并定义了特殊清单broadFileSystemAccess,因此我可以直接从PC访问所有文件 我通过将ListView的ItemsSource设置为observeCollection变量来填充ListView(更多详细信息见下文)。点击后,我想更改它 在classsomeModel中,我将图像和内容的路径设置为BitmapImage。我这样做是因为,该文件位于我电脑上的某个路径上,例如e:\pix\some path\image.jpg,然后将该文件读入Bi

我为自己提供了一个C#UWP解决方案,并定义了特殊清单
broadFileSystemAccess
,因此我可以直接从PC访问所有文件

我通过将
ListView
ItemsSource
设置为
observeCollection
变量来填充
ListView
(更多详细信息见下文)。点击后,我想更改它

在class
someModel
中,我将图像和内容的路径设置为
BitmapImage
。我这样做是因为,该文件位于我电脑上的某个路径上,例如
e:\pix\some path\image.jpg
,然后将该文件读入
BitmapImage
,以便以后将其绑定为图像源。这是超慢和低效的,但它现在的工作,我会改变它在未来

class someModel
{
   public string ImagePath { get; set; }
   public BitmapImage ImageContent { get; set; }
}
以及我的XAML图像:

<Image DataContext="{x:Bind Mode=TwoWay}" Source="{x:Bind ImageContent, Mode=OneWay}" Width="400" Stretch="UniformToFill" Tapped="lvImageEditImage_Tapped" />
虽然我现在获得了路径,但图像内容
bitmapImage
不会更改查看的图像


为什么图像不更新到新的
BitmapImage
内容?

请将
INotifyPropertyChanged
接口添加到数据模型中

公共类someModel:INotifyPropertyChanged
{
私有位图图像\u图像内容;
公共位图图像图像内容
{
获取=>\u图像内容;
设置
{
_图像内容=值;
OnPropertyChanged();
}
}
公共字符串ImagePath{get;set;}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
继承
INotifyPropertyChanged
接口后,可以使用
{x:Bind ImageContent,Mode=OneWay}
完成绑定。修改数据源时,会通知UI界面进行更改


致意。

请将
INotifyPropertyChanged
接口添加到数据模型中

公共类someModel:INotifyPropertyChanged
{
私有位图图像\u图像内容;
公共位图图像图像内容
{
获取=>\u图像内容;
设置
{
_图像内容=值;
OnPropertyChanged();
}
}
公共字符串ImagePath{get;set;}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
继承
INotifyPropertyChanged
接口后,可以使用
{x:Bind ImageContent,Mode=OneWay}
完成绑定。修改数据源时,会通知UI界面进行更改

致以最良好的祝愿

var picker = new Windows.Storage.Pickers.FileOpenPicker
{
    ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
    SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
};

StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
    var item = (sender as Image).DataContext as someModel;
    item.ImagePath = file.Path;

    BitmapImage bitmapImage = new BitmapImage();
    using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
    {
        await bitmapImage.SetSourceAsync(fileStream);
    }
    item.ImageContent = bitmapImage;
}