【问题标题】:INotifyPropertyChanged doesn't work (UWP)INotifyPropertyChanged 不起作用 (UWP)
【发布时间】:2016-07-15 09:35:31
【问题描述】:

我需要在View 上刷新Image 这是我的 ViewPage:

<Image Grid.Row="1"
Grid.Column="1" 
x:Name="OriginalImg"
Source="{Binding Orig}"
DataContext="{StaticResource MainViewModel}"/>

我正在使用 MVVMLibs 包。这是我的 ViewModel:

public class MainViewModel: ViewModelBase
{
    private WriteableBitmap original = new WriteableBitmap(1280,720);
    private WriteableBitmap temp = new WriteableBitmap(1280,720);

    public WriteableBitmap Orig
    {
        get { return original; }
        set
        {
            this.original = value;
            base.RaisePropertyChanged("Orig");
        }
    }
    public async Task<bool> ApplyEffectAsync(StorageFile file)
    {
      fileStream = await file.OpenAsync(FileAccessMode.Read);
      temp.SetSource(fileStream);
      Orig = temp;
    }
}

但是我的页面上的Image 没有显示。我有什么问题?

【问题讨论】:

  • ApplyEffectAsync 在哪里调用?
  • 它在另一个类中用ICommand 调用,并且此方法可以正常工作。下一个 VS 转到我的二传手并调用 RaisePropertyChanged 但视图上没有显示任何内容
  • 我贴出来了。我用Source="{Binding Orig}"
  • 好吧,我们很清楚,我一直在 UWP 中使用 INotifyPropertyChanged。它工作大声笑...其次,你的输出窗口说什么?是否有任何 BindingExpression 错误?图片的实际来源路径是什么?
  • 我没有任何错误或异常。我使用FilePicker 打开我的图像,然后将其放入StorageFile 并使用FileStream 放入WriteableBitmap

标签: c# windows visual-studio xaml uwp


【解决方案1】:

问题是你并没有真正实例化一个新的 WriteableBitmap,只是改变它的源。所以虽然它可能第一次工作,但它肯定不会工作,因为依赖属性管理器不会知道你的图像改变,除非它的实例改变。

尝试在 ApplyEffectAsync 方法中创建您的临时 WriteableBitmap。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 2011-06-26
    • 2011-11-26
    • 1970-01-01
    • 2017-08-06
    • 2020-03-30
    • 1970-01-01
    相关资源
    最近更新 更多