【问题标题】:WPF, C#, MVVM Notify ViewModel dynamically in changes from static vars in ModelWPF、C#、MVVM 在模型中静态变量的变化中动态通知 ViewModel
【发布时间】:2018-09-11 20:08:24
【问题描述】:

我有 ViewModel 可以实现一些功能。功能由按钮启动,我有按钮点击命令。

ViewModel.cs

class WindowViewModel : INotifyPropertyChanged
{
    public WindowViewModel()
    {
        canExecute = true;
    }

    public ICommand ApplyKMMCommand //command for button click, works great, tested
    {
        get
        {
            return applyKMMCommand ?? (applyKMMCommand = new Commands.CommandHandler(() => 
                                                       ApplyKMMToNewImage(), canExecute));
        }
    }

    private bool canExecute;
    private ICommand applyKMMCommand;

    public void ApplyKMMToNewImage()
    {
        ApplyKMM.Init(); //algorithm name
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public BitmapImage DisplayedImage //displaying image that i work with
    {
        get { return Bitmaps.Filepath; }
        set { Bitmaps.Filepath = value; NotifyPropertyChanged(nameof(DisplayedImage)); }
    }
}

现在,我的ApplyKMM.Init()

class ApplyKMM
{

    public static void Init()
    {
        Bitmaps.Filepath = //do some thing with it...
    }
}

还有我的Models.Bitmaps.cs

    static public BitmapImage Filepath
    {
        get { return filepath; }
        set { filepath = value; }
    }

    static private BitmapImage filepath{ get; set; }

问题是,当我将ApplyKMM.Init 绑定到 View 的 Image 控件设置为不会更改其值时。

没有ApplyKMM,我可以在 ViewModel 中做那件事:

DisplayedImage = //do things with bitmap...

然后,在视图中显示的Image 发生变化(在使用该图像进行操作之后)。

你能告诉我,如何通知 ViewModel,Models 的代码 filepath 的某处发生了变化吗?

编辑:

View 中的绑定看起来像标准绑定:

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

按钮点击也可以,我只有Models-&gt;ApplyKMM-&gt;ViewModel之间的通信有问题

EDIT2:

属性Filepath 存储在Models 文件夹中,而不是函数ApplyKMM 所在的文件夹。查看我的编辑,我尝试制作如下内容:

Models -&gt; ApplyKMM -&gt; ViewModel。从模特那里,我得到Filepath。然后,我使用另一个命名空间中的函数ApplyKMM。然后,在使用ApplyKMM 函数处理位图后,我想以某种方式通知ViewModel,Model 上的工作已完成(例如,转换为灰度),我想在 VM 中显示该灰度图像。当我想做Model -&gt; ViewModel(ApplyKMM 在 VM 类中)但我想从ViewModel 移出ApplyKMM 时,它可以工作。而当我的星星开始时。

【问题讨论】:

  • 如果有人投反对票,最好知道原因,因为 Stack 中的示例不符合我的需要,我无法根据我的需要重写它

标签: c# wpf mvvm binding


【解决方案1】:

您似乎想在静态属性更改时发出通知。为此,您可以使用StaticPropertyChanged 事件。

class ApplyKMM
{
    #region Properties
    static BitmapImage _Filepath;
    public static BitmapImage Filepath
    {
        get { return _Filepath; }
        set { if (_Filepath != value) { _Filepath = value; NotifyPropertyChanged(nameof(Filepath)); } }
    }
    #endregion

    #region Static NotifyPropertyChanged
    public static void NotifyStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }

    public void NotifyAllStaticPropertyChanged()
    {
        NotifyStaticPropertyChanged(string.Empty);
    }

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    #endregion
}

请注意,这在 WPF 4.5 版中可用。
您可能还会发现 this question 很有趣。

【讨论】:

  • 这在我的场景中不起作用,因为属性Filepath 存储在Models 文件夹中,而不是函数ApplyKMM 所在的文件夹中。查看我的编辑,我尝试制作类似:Models -&gt; ApplyKMM -&gt; ViewModel。从模特那里,我得到Filepath。然后,我使用另一个命名空间中的函数 ApplyKMM。然后,在使用ApplyKMM func 处理位图后,我想以某种方式通知ViewModel,Model 上的工作已完成(例如,转换为灰度),我想在 VM 中显示该灰度图像
【解决方案2】:

基本上,将静态变量的更改通知给实例并不是一个好习惯。

然后,让我们看看你的代码:

Bitmaps类没有实现INotifyPropertyChanged,所以当Filepath发生变化时,什么都不会通知(当然是静态属性)

在您的情况下,您应该使用局部变量来保存DisplayedImages。然后 DisplayedImage 上的更改应该由绑定更新。

BitmapImage _displayedImage;
public BitmapImage DisplayedImage 
{
    get { return displayedImage; }
    set { displayedImage = value; NotifyPropertyChanged(nameof(DisplayedImage)); }
}

【讨论】:

  • 我知道我可以在本地制作,但是如何以这种方式通知 VM:VM -&gt; ApplyKMM -&gt; Model?
  • 我不想在 VM 和 Model 中存储相同的值
  • 那么你需要在Models.Bitmaps.cs类中保存一个静态变量,并将WindowViewModel的实例传递给它。当 Filepath 发生变化时,您可以通过静态实例更新 DisplayedImage。 (好吧,这不是推荐的......)
猜你喜欢
  • 1970-01-01
  • 2017-05-13
  • 2013-06-03
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2021-07-28
  • 1970-01-01
相关资源
最近更新 更多