Image 将列表视图项模板图像绑定到ObservableCollection不工作

Image 将列表视图项模板图像绑定到ObservableCollection不工作,image,listview,binding,uwp,Image,Listview,Binding,Uwp,我知道有很多关于这个的问题,我搜索了很多,尝试了所有我找到的关于这个的东西,但是我不能让它工作。 简单地说,由于某些原因,我无法在ListView项目模板中显示图像 我有一个ItemViewModel类: public class ItemViewModel : BaseViewModel, IItemViewModel { public ItemViewModel() { if (dalInterface == null) {

我知道有很多关于这个的问题,我搜索了很多,尝试了所有我找到的关于这个的东西,但是我不能让它工作。 简单地说,由于某些原因,我无法在ListView项目模板中显示图像

我有一个ItemViewModel类:

public class ItemViewModel : BaseViewModel, IItemViewModel
{
    public ItemViewModel()
    {
        if (dalInterface == null)
        {
            dalInterface = ApplicationContext.Container.Resolve<IDalInterface>();
        }
        if (eventCenter == null)
        {
            eventCenter = ApplicationContext.Container.Resolve<IEventCenter>();
        }

        SaveCommand = new Command(SaveChanges, true);
        DeleteCommand = new Command(RemoveItem, true);
        AddNewItemCommand = new Command(AddNewItem, true);
    }

    public ICommand SaveCommand { get; set; }

    public ICommand DeleteCommand { get; set; }

    public ICommand AddNewItemCommand { get; set; }

    private Item data;

    public int ID { get; private set; }

    private string title;
    public string Title
    {
        get { return title; }
        set
        {
            title = value;
            NotifyPropertyChanged("Title");
        }
    }

    private string author;
    public string Author
    {
        get { return author; }
        set
        {
            author = value;
            NotifyPropertyChanged("Author");
        }
    }

    private string shortDescription;
    public string ShortDescription
    {
        get { return shortDescription; }
        set
        {
            shortDescription = value;
            NotifyPropertyChanged("ShortDescription");
        }
    }

    private string buyPrice;
    public string BuyPrice
    {
        get { return buyPrice; }
        set
        {
            buyPrice = value;
            NotifyPropertyChanged("BuyPrice");
        }
    }

    private string borrowPrice;
    public string BorrowPrice
    {
        get { return borrowPrice; }
        set
        {
            borrowPrice = value;
            NotifyPropertyChanged("BorrowPrice");
        }
    }

    private int quantity;
    public int Quantity
    {
        get { return quantity; }
        set
        {
            quantity = value;
            NotifyPropertyChanged("Quantity");
        }
    }


    private string detailedDescription;
    public string DetailedDescription
    {
        get { return detailedDescription; }
        set
        {
            detailedDescription = value;
            NotifyPropertyChanged("DetailedDescription");
        }
    }

    private string imagePath;
    public string ImagePath
    {
        get { return imagePath; }
        set
        {
            imagePath = value;
            NotifyPropertyChanged("ImagePath");
        }
    }
    private Image image;
    public Image Image
    {
        get { return image; }
        set
        {
            image = value;
            NotifyPropertyChanged("Image");
        }
    }

    public void SetData(Item item)
    {
        data = item;
        ID = data.ID;
        Author = data.Author;
        Title = data.Title;
        Quantity = data.Quantity;
        ShortDescription = data.ShortDescription;
        DetailedDescription = data.DetailedDescription;
        BuyPrice = data.BuyPrice;
        BorrowPrice = data.BorrowPrice;
        Image = GetImage(data.ImagePath);
    }

    private Image GetImage(string imagePath)
    {
        var imageUri = new Uri(imagePath, UriKind.RelativeOrAbsolute);
        var bitmapImage = new BitmapImage(imageUri);
        var image = new Image
        {
            Source = bitmapImage
        };

        return Image;
    }
    private void SaveChanges()
    {
        UpdateChanges(data);
        dalInterface.UpdateItem(data);
    }

    private void RemoveItem()
    {
        dalInterface.RemoveItem(data);
    }

    private void AddNewItem()
    {
        var newItem = new Item();
        if (AllDataCorrect())
        {
            UpdateChanges(newItem);
            dalInterface.AddNewItem(newItem);
            eventCenter.Publish(new AddItemEventArgs { OperationSuccess = true });
        }
        else
        {
            eventCenter.Publish(new AddItemEventArgs { OperationSuccess = false });
        }
    }

    private void UpdateChanges(Item itemToUpdate)
    {
        itemToUpdate.Author = Author;
        itemToUpdate.Title = Title;
        itemToUpdate.BorrowPrice = BorrowPrice;
        itemToUpdate.BuyPrice = BuyPrice;
        itemToUpdate.DetailedDescription = DetailedDescription;
        itemToUpdate.ShortDescription = ShortDescription;
        itemToUpdate.Quantity = Quantity;
        itemToUpdate.ImagePath = ImagePath;
    }

    private bool AllDataCorrect()
    {
        float val = -1.0F;

        float.TryParse(BuyPrice, out val);
        if (val <= 0.0F)
        {
            return false;
        }

        float.TryParse(BorrowPrice, out val);
        if (val <= 0.0F)
        {
            return false;
        }

        if ((ShortDescription == string.Empty) ||
            (DetailedDescription == string.Empty) ||
            (Author == string.Empty) ||
            (Title == string.Empty)
          )
        {
            return false;
        }

        if (Quantity <= 0)
        {
            return false;
        }

        return true;
    }

    public void Clear()
    {
        Author = string.Empty;
        Title = string.Empty;
        ImagePath = string.Empty;
        ShortDescription = string.Empty;
        DetailedDescription = string.Empty;
        BuyPrice = string.Empty;
        BorrowPrice = string.Empty;
        Quantity = 0;
    }
}
公共类ItemViewModel:BaseViewModel、IIItemViewModel
{
公共项目视图模型()
{
if(dainterface==null)
{
daInterface=ApplicationContext.Container.Resolve();
}
if(eventCenter==null)
{
eventCenter=ApplicationContext.Container.Resolve();
}
SaveCommand=new命令(SaveChanges,true);
DeleteCommand=新命令(removietem,true);
AddNewItemCommand=新命令(AddNewItem,true);
}
公共ICommand SaveCommand{get;set;}
公共ICommand DeleteCommand{get;set;}
公共ICommand AddNewItemCommand{get;set;}
私人物品数据;
public int ID{get;private set;}
私有字符串标题;
公共字符串标题
{
获取{返回标题;}
设置
{
标题=价值;
通知财产变更(“所有权”);
}
}
私有字符串作者;
公共字符串作者
{
获取{返回作者;}
设置
{
作者=价值;
NotifyPropertyChanged(“作者”);
}
}
私有字符串短描述;
公共字符串简短描述
{
获取{return shortDescription;}
设置
{
shortDescription=值;
NotifyPropertyChanged(“简短描述”);
}
}
私人字符串买价;
公共字符串买价
{
获取{返回买价;}
设置
{
购买价格=价值;
通知财产变更(“买价”);
}
}
私人价格;
公共字符串价格
{
获取{返回价格;}
设置
{
价格=价值;
通知物业变更(“借款价格”);
}
}
私人整数数量;
公共整数
{
获取{返回数量;}
设置
{
数量=价值;
NotifyPropertyChanged(“数量”);
}
}
私有字符串详细描述;
公共字符串详细说明
{
获取{return detailedDescription;}
设置
{
详细描述=数值;
NotifyPropertyChanged(“详细说明”);
}
}
私有字符串路径;
公共字符串映像路径
{
获取{return imagePath;}
设置
{
imagePath=值;
NotifyPropertyChanged(“ImagePath”);
}
}
私有图像;
公众形象
{
获取{返回图像;}
设置
{
形象=价值;
NotifyPropertyChanged(“图像”);
}
}
公共无效设置数据(项目)
{
数据=项目;
ID=data.ID;
作者=数据。作者;
Title=data.Title;
数量=数据。数量;
ShortDescription=data.ShortDescription;
DetailedDescription=数据。DetailedDescription;
BuyPrice=data.BuyPrice;
BorrowPrice=data.BorrowPrice;
Image=GetImage(data.ImagePath);
}
私有图像GetImage(字符串imagePath)
{
var imageUri=新Uri(imagePath,UriKind.RelativeOrAbsolute);
var bitmapImage=新的bitmapImage(imageUri);
var图像=新图像
{
源=位图图像
};
返回图像;
}
私有void SaveChanges()
{
更新更改(数据);
dainterface.UpdateItem(数据);
}
私有void removietem()
{
dainterface.removietem(数据);
}
私有void AddNewItem()
{
var newItem=newItem();
if(AllDataCorrect())
{
更新更改(新项目);
dainterface.AddNewItem(newItem);
Publish(新的AddItemEventArgs{OperationSuccess=true});
}
其他的
{
Publish(新的AddItemEventArgs{OperationSuccess=false});
}
}
私有void更新更改(itemToUpdate)
{
itemToUpdate.Author=作者;
itemToUpdate.Title=标题;
itemToUpdate.BorrowPrice=借入价格;
itemToUpdate.BuyPrice=购买价格;
itemToUpdate.DetailedDescription=详细描述;
itemToUpdate.ShortDescription=简短描述;
itemToUpdate.Quantity=数量;
itemToUpdate.ImagePath=ImagePath;
}
私有bool AllDataCorrect()
{
浮点值=-1.0F;
float.TryParse(买入价,卖出价);

if(val不能将图像控件用作另一图像控件的
源属性的值

改用
图像源

private ImageSource image;
public ImageSource Image
{
    get { return image; }
    set
    {
        image = value;
        NotifyPropertyChanged("Image");
    }
}
并将
GetImage
方法更改为:

private ImageSource GetImage(string imagePath)
{
    return new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
}

也就是说,您可以简单地删除
Image
属性并直接绑定到
ImagePath
。内置类型转换将自动从路径字符串转换为ImageSource

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


还请注意,
Mode=TwoWay
对此绑定没有意义。

不能将图像控件用作另一个图像控件的
源属性的值

改用
图像源

private ImageSource image;
public ImageSource Image
{
    get { return image; }
    set
    {
        image = value;
        NotifyPropertyChanged("Image");
    }
}
并将
GetImage
方法更改为:

private ImageSource GetImage(string imagePath)
{
    return new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
}

也就是说,您可以简单地删除
Image
属性并直接绑定到
ImagePath
。内置类型转换将自动从路径字符串转换为ImageSource

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


还要注意的是,
Mode=TwoWay
对于这个绑定没有任何意义。

因为我一次又一次地搜索这个问题,并尝试了很多sugg
public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        PickFileCommand = new ActionCommand(PickFile, true);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public ICommand PickFileCommand { get; set; }

    private BitmapImage imageSrc;
    public BitmapImage ImageSrc
    {
        get { return imageSrc; }
        set
        {
            imageSrc = value;
            NotifyPropertyChanged("ImgSource");
        }
    }

    private async void PickFile()
    {
        var filePicker = new FileOpenPicker
        {
            SuggestedStartLocation = PickerLocationId.PicturesLibrary
        };
        filePicker.FileTypeFilter.Add(".jpg");
        filePicker.FileTypeFilter.Add(".jpeg");
        filePicker.FileTypeFilter.Add(".png");

        StorageFile file = await filePicker.PickSingleFileAsync();

        if (file != null)
        {
            var stream = await file.OpenAsync(FileAccessMode.Read);
            var bitmap = new BitmapImage
            {
                UriSource = new Uri(file.Path, UriKind.Absolute)
            };
            await bitmap.SetSourceAsync(stream);

            ImageSrc = bitmap;
        }
    }

    protected void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}
 public sealed partial class MainPage : Page
{
    private ViewModel dataContext;
    public MainPage()
    {
        this.InitializeComponent();
        dataContext = new ViewModel();

        DataContext = dataContext;
    }

    **private void PageLoaded(object sender, RoutedEventArgs e)
    {
        if (DataContext is ViewModel dc)
        {
            dc.PropertyChanged += Dc_PropertyChanged;
        }
    }
    private void Dc_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if(e.PropertyName.Equals("ImgSource"))
        {
            if (DataContext is ViewModel dc)
            {
                ShowImage.Source = dc.ImageSrc;
            }
        }
    }**
}