【发布时间】: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->ApplyKMM->ViewModel之间的通信有问题
EDIT2:
属性Filepath 存储在Models 文件夹中,而不是函数ApplyKMM 所在的文件夹。查看我的编辑,我尝试制作如下内容:
Models -> ApplyKMM -> ViewModel。从模特那里,我得到Filepath。然后,我使用另一个命名空间中的函数ApplyKMM。然后,在使用ApplyKMM 函数处理位图后,我想以某种方式通知ViewModel,Model 上的工作已完成(例如,转换为灰度),我想在 VM 中显示该灰度图像。当我想做Model -> ViewModel(ApplyKMM 在 VM 类中)但我想从ViewModel 移出ApplyKMM 时,它可以工作。而当我的星星开始时。
【问题讨论】:
-
如果有人投反对票,最好知道原因,因为 Stack 中的示例不符合我的需要,我无法根据我的需要重写它