在MVVM和WPF中重新加载Json内容

在MVVM和WPF中重新加载Json内容,wpf,mvvm,Wpf,Mvvm,我有一个叫FieldModel的模型。在ViewModel中,我通过json文件解析设置其属性,如下所示: foreach (var field in innerClass.Item2.Properties) { FieldView fieldView = new FieldView(field); fieldView.ClassName = classView

我有一个叫FieldModel的模型。在ViewModel中,我通过json文件解析设置其属性,如下所示:

foreach (var field in innerClass.Item2.Properties)
                    {
                        FieldView fieldView = new FieldView(field);
                        fieldView.ClassName = classView.ClassName; 
                        fieldView.IsAbstract = classView.IsAbstract;
                        FieldViewItems.Add(fieldView);
                    }

我的问题是:如何使用“重新加载”按钮正确地进行绑定,以便在修改json文件时重新加载该文件的内容?

首先实现一个命令类,我更喜欢这样:

public class GeneralCommand : ICommand
{
    private Action ToBeExecutedAction;
    private Func<bool> ExecutionValidatorFunc;

    public GeneralCommand(Action toBeExecutedAction, Func<bool> executionValidatorFunc)
    {
        ToBeExecutedAction = toBeExecutedAction;
        ExecutionValidatorFunc = executionValidatorFunc;
    }

    public bool CanExecute(object parameter)
    {
        return ExecutionValidatorFunc();
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        ToBeExecutedAction();
    }
}
公共类通用命令:ICommand
{
被执行的私人行动;
私有函数executionvalidator Func;
公共常规命令(要执行的操作Action,Func ExecutionValidator Func)
{
ToBeExecutedAction=ToBeExecutedAction;
ExecutionValidatorFunc=ExecutionValidatorFunc;
}
公共布尔CanExecute(对象参数)
{
返回ExecutionValidatorFunc();
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
ToBeExecutedAction();
}
}
现在,在ViewModel类中,实现如下内容:

public class FieldModel : INotifyPropertyChanged
{
    private GeneralCommand _generalCommand;

    public FieldModel()
    {
        Action action = new Action(ChangeValue);
        _generalCommand = new GeneralCommand(action, new Func<bool>(() => true));
    }

    public ICommand ReloadValues
    {
        get
        {
            return _generalCommand;
        }
    }

    string _jsonText;
    public string JsonText
    {
        get
        {
            return _jsonText;
        }
    }

    private void ChangeValue()
    {
        //Change JsonText here 

        //Then raise event change to be updated
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("TextJson"));//Here fill property name
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
公共类字段模型:INotifyPropertyChanged
{
私人总司令(普通司令);;
公共领域模型()
{
动作动作=新动作(变更值);
_generalCommand=新的generalCommand(操作,新函数(()=>true));
}
公共ICommand重新加载值
{
得到
{
返回(通用命令);;
}
}
字符串_jsonText;
公共字符串JsonText
{
得到
{
返回_jsonText;
}
}
私有void ChangeValue()
{
//在此处更改JsonText
//然后引发要更新的事件更改
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“TextJson”);//这里填写属性名称
}
}
公共事件属性更改事件处理程序属性更改;
}
然后从Xaml将重新加载按钮绑定到ViewModel对象内的命令属性
ReloadValues
,并使用WPF控件(例如
Textbox
)绑定
JsonText
属性


希望这是有用的。

您的
FieldViewItems
是带有支持字段的属性吗?它是否正在引发
属性更改
事件?它是一个可观察的集合。FieldView是我的模型。您也可以使用
FileSystemWatcher
自动监视文件的更改。并通过写入:PropertyChangedEventArgs(“PropertyName”)正确更改属性字段?确定。但是,如果打开json文件时发生了“//Change JsonText here”,而在C#code中没有发生,该怎么办?该方法是否仍然有效?如果您的意思是可以从外部代码更改,则每次重新打开文件并显示其内容时单击“重新加载”按钮时