【问题标题】:in a sync await method the image property dont binding with the UI在异步等待方法中,图像属性未与 UI 绑定
【发布时间】:2018-10-08 15:39:47
【问题描述】:

所以我尝试使用 async-await 与 bitmapImage 列表进行绑定,但由于某种原因,我在 UI 上看不到图像。

在我尝试更新属性的循环中,当我这样做时:

ImageSource = imageList[2];

或 0 到 9 之间的任何其他数字,则图像将显示在 UI 屏幕中。

这是我的代码的一部分,直到没有发生绑定的循环:

编辑:这是问题:

    private BitmapImage imageSource = null;
    public BitmapImage ImageSource
    {
        get
        {
            return imageSource;
        }
        set
        {
            imageSource = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("MessagePerSec");
        }
    }

【问题讨论】:

    标签: c# wpf binding async-await bitmapimage


    【解决方案1】:

    我直接看到了一个问题。 你有错误的字符串:

    public BitmapImage **ImageSource**
    {
        get
        {
            return imageSource;
        }
        set
        {
            imageSource = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("**MessagePerSec")**;
        }
    }
    

    MessagePerSec 而不是 ImageSource。顺便说一句,这本身就是一个坏名字。

    【讨论】:

    • 除非您在使用 .net3.5 时遇到问题,否则您应该在 msdn 示例中使用 inotifypropertychanged 的​​实现。这使用 callermembername 来计算属性的名称,并完全避免编写错误的“魔术”字符串。 nameof() 是另一种选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多