using PLCSharp.Core.Prism;
using PLCSharp.Models;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.Windows;
namespace PLCSharp.VVMs.GlobalVariables
{
///
/// Variables模型
///
[Model]
public partial class VariablesModel : ModelBase
{
///
/// Variables模型
///
public VariablesModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
{
}
///
/// 全局模型
///
public GlobalModel GlobalModel { get; set; }
///
/// 初始化
///
/// 全局模型
public void Init(GlobalModel globalModel)
{
GlobalModel = globalModel;
foreach (var item in Variables)
{
item._DatasContext = _DatasContext;
}
foreach (var item in SystemVariables)
{
item._DatasContext = _DatasContext;
}
}
internal void LoadRecipe(Guid CurrentRecipeID)
{
Variables.Clear();
var variables = _DatasContext.Variables.Where(c => c.RecipeID == CurrentRecipeID);
foreach (var item in variables)
{
if (item.RetainPersistent)
{
item.Value = Variable.DynamicValue(item.ValueStr, item.Type);
}
else
{
item.Value = Variable.DynamicValue(item.DefaultValueStr, item.Type);
item.DefaultValue = item.Value;
}
Variables.Add(item);
}
//加载当前配方的系统变量列表
SystemVariables.Clear();
var systemVariables = _DatasContext.SystemVariables.Where(c => c.RecipeID == CurrentRecipeID);
foreach (var item in systemVariables)
{
if (item.RetainPersistent)
{
item.Value = Variable.DynamicValue(item.ValueStr, item.Type);
}
else
{
item.Value = Variable.DynamicValue(item.DefaultValueStr, item.Type);
item.DefaultValue = item.Value;
}
SystemVariables.Add(item);
}
}
///
/// 局部变量集合
///
public ObservableCollection Variables { get; set; } = [];
public ObservableCollection SystemVariables { get; set; } = [];
private Variable _SelectedVariable;
///
/// 选中的全局变量
///
public Variable SelectedVariable
{
get { return _SelectedVariable; }
set { SetProperty(ref _SelectedVariable, value); }
}
private DelegateCommand