C# 如何在MVVM灯光下从视图和模型更新WPF窗口viewmodel属性
我的应用程序有一些工作模式,用户可以从模型和UI中进行更改。 当我启动应用程序时,它显示默认模式正确(从模型更新的数据)。但当我从UI更改选定项时,它会显示选定项一段时间,然后取消选择它(从UI更新,然后从模型更新)。 我在两种情况下划分了更新逻辑 首先,来自UI的更新将调用模型更新。 第二,来自模型的更新UI不应调用模型更新。 我犯了什么错? 请参阅下面的代码 看法C# 如何在MVVM灯光下从视图和模型更新WPF窗口viewmodel属性,c#,wpf,mvvm,mvvm-light,C#,Wpf,Mvvm,Mvvm Light,我的应用程序有一些工作模式,用户可以从模型和UI中进行更改。 当我启动应用程序时,它显示默认模式正确(从模型更新的数据)。但当我从UI更改选定项时,它会显示选定项一段时间,然后取消选择它(从UI更新,然后从模型更新)。 我在两种情况下划分了更新逻辑 首先,来自UI的更新将调用模型更新。 第二,来自模型的更新UI不应调用模型更新。 我犯了什么错? 请参阅下面的代码 看法 视图模型 private NamedInt _selectedAlgorithm; priv
视图模型
private NamedInt _selectedAlgorithm;
private ObservableCollection<NamedInt> _algorithmCollection;
public NamedInt SelectedAlgorithm
{
get { return this._selectedAlgorithm; }
set { Set(() => SelectedAlgorithm, ref this._selectedAlgorithm, value);
if (this._needUpdate && Core.Kernel.Instance.PluginController.ActiveConverter != null &&
this._selectedAlgorithm != null)
{
Core.Kernel.Instance.PluginController.ActiveConverter.SetActiveAlgorithm(this._selectedAlgorithm.Id);
}
}
}
public ObservableCollection<NamedInt> AlgorithmCollection
{
get { return this._algorithmCollection; }
set { Set(() => AlgorithmCollection, ref this._algorithmCollection, value);
}
}
private NamedInt\u selected算法;
私有可观测集合算法集合;
公共名称力选择算法
{
获取{返回此项。\ u selectedAlgorithm;}
set{set(()=>SelectedAlgorithm,ref this.\u SelectedAlgorithm,value);
if(this.\u needUpdate&&Core.Kernel.Instance.pluginControl.ActiveConverter!=null&&
这是。\u selectedAlgorithm!=null)
{
Core.Kernel.Instance.pluginControl.ActiveConverter.SetActiveAlgorithm(this.\u selectedAlgorithm.Id);
}
}
}
公共可观测集合算法集合
{
获取{返回此。_algorithmCollection;}
set{set(()=>AlgorithmCollection,ref this.\u AlgorithmCollection,value);
}
}
我听模型事件,在必要时更新ViewModel。在ViewModel的构造函数中
Messenger.Default.Register<SoftwareSettingsUpdateMessage>(this, ReceiveUpdateMessage);
private void ReceiveUpdateMessage(SoftwareSettingsUpdateMessage action)
{
if (action.SettingType.HasFlag(SoftwareSettingsType.Algorithm))
{
RefreshAlgorithms();
}
this._needUpdate = true;
}
private void RefreshAlgorithms()
{
var converter = Core.Kernel.Instance.PluginController.ActiveConverter;
var algorithms = converter?.Algorithms;
if (algorithms != null)
{
AlgorithmCollection = new ObservableCollection<NamedInt>(
algorithms.Select((x, index) => new NamedInt(x, index))
);
var activeId = converter.ActiveAlgorithmId;
if (activeId >= 0 && activeId < AlgorithmCollection.Count)
{
this._needUpdate = false;
SelectedAlgorithm = AlgorithmCollection[activeId];
this._needUpdate = true;
}
else
{
SelectedAlgorithm = null;
}
}
else
{
AlgorithmCollection = new ObservableCollection<NamedInt>();
SelectedAlgorithm = null;
}
IsActive = true;
}
Messenger.Default.Register(这是ReceiveUpdateMessage);
private void ReceiveUpdateMessage(软件设置SupdateMessage操作)
{
if(action.SettingType.HasFlag(SoftwareSettingsType.Algorithm))
{
刷新算法();
}
这是。_needUpdate=true;
}
私有void刷新算法()
{
var converter=Core.Kernel.Instance.PluginController.ActiveConverter;
var算法=转换器?算法;
if(算法!=null)
{
AlgorithmCollection=新的ObservableCollection(
算法。选择((x,索引)=>newnamedint(x,索引))
);
var activeId=converter.ActiveAlgorithmId;
if(activeId>=0&&activeId
Messenger.Default.Register<SoftwareSettingsUpdateMessage>(this, ReceiveUpdateMessage);
private void ReceiveUpdateMessage(SoftwareSettingsUpdateMessage action)
{
if (action.SettingType.HasFlag(SoftwareSettingsType.Algorithm))
{
RefreshAlgorithms();
}
this._needUpdate = true;
}
private void RefreshAlgorithms()
{
var converter = Core.Kernel.Instance.PluginController.ActiveConverter;
var algorithms = converter?.Algorithms;
if (algorithms != null)
{
AlgorithmCollection = new ObservableCollection<NamedInt>(
algorithms.Select((x, index) => new NamedInt(x, index))
);
var activeId = converter.ActiveAlgorithmId;
if (activeId >= 0 && activeId < AlgorithmCollection.Count)
{
this._needUpdate = false;
SelectedAlgorithm = AlgorithmCollection[activeId];
this._needUpdate = true;
}
else
{
SelectedAlgorithm = null;
}
}
else
{
AlgorithmCollection = new ObservableCollection<NamedInt>();
SelectedAlgorithm = null;
}
IsActive = true;
}