我是WPF + MVVM的新手,并且在遍历 View 模型时遇到了麻烦。

我有一个名为FSystem的对象,它包含很多从XML填充的列表。

public class FSystem : ObservableObject
{
    public List<FUser> _userList;
    public List<FZone> _zoneList;
    public List<FSource> _sourceList;

    public string _projectName { get; set; }
    private string _projectVersion { get; set; }
    private string _processorIp { get; set; }

    private bool _isMultiLingualModeOn { get; set; }

    private int _systemIncludeLighting { get; set; }
    private int _systemIncludeWindowsTreatments { get; set; }
    private int _systemIncludeSip { get; set; }
    private int _systemIncludeCamaras { get; set; }

    public FSystem()
    {
        UserList = new List<FUser>();
    }
}

这是XMLParser,当用户将XML加载到应用程序时被调用。
public static class XMLParsers
{
    public static FSystem ParseByXDocument(string xmlPath)
    {
        var fSystem = new FSystem();

        XDocument doc = XDocument.Load(xmlPath);

        XElement fSystemElement = doc.Element("FSystem");

        if (fSystemElement != null)
        {
            fSystem.ProjectName = fSystemElement.Element("ProjectName").Value;
            fSystem.ProjectVersion = fSystemElement.Element("ProjectVersion").Value;
            fSystem.ProcessorIp = fSystemElement.Element("ProcessorIP").Value;
            fSystem.ProcessorFilePath = fSystemElement.Element("ProcessorFilePath").Value;
            fSystem.SystemIncludeLighting = Convert.ToInt16(fSystemElement.Element("SystemIncludeLighting").Value);
            fSystem.SystemIncludeSip = Convert.ToInt16(fSystemElement.Element("SystemIncludeLighting").Value);
            fSystem.SystemIncludeCamaras = Convert.ToInt16(fSystemElement.Element("SystemIncludeCameras").Value);
        }

        fSystem.UserList = (from user in doc.Descendants("FUser")
                                 select new FUser()
                                 {
                                     Id = user.Element("Id").Value,
                                     Name = user.Element("Name").Value,
                                     Icon = user.Element("IconColour").Value,
                                     Pin = user.Element("UserPin").Value,
                                     IsPinEnabled = Convert.ToBoolean(Convert.ToInt16(user.Element("UserPinEnabled").Value)),
                                     ListIndex = user.Element("ListIndex").Value
                                 }).ToList();

        return fSystem;
    }
}

这是MainViewModel,下面是包含加载XML的命令以及我希望在其他 View 模型中使用的属性FSystem。
public class MainViewModel : ViewModel
{
    private Fystem fSystem;
    public FSystem FSystem
    {
        get { return fSystem; }
        private set
        {
            fSystem = value;
            NotifyPropertyChanged("FSystem");
        }
    }

    public MainViewModel()
    {
        InitiateState();
        WireCommands();
    }

    private void InitiateState()
    {
        FSystem = new FSystem();
    }

    private void WireCommands()
    {
        XDocumentLoadCommand = new RelayCommand(XDocumentLoad) {IsEnabled = true};

        ClearDataCommand = new RelayCommand(ClearData) {IsEnabled = true};
    }
public RelayCommand XDocumentLoadCommand { get; private set; }
    private void XDocumentLoad()
    {
        var openDlg = new OpenFileDialog
        {
            Title = "Open .FAS",
            DefaultExt = ".fas",
            Filter = "F System Files (*.fas)|*.fas",
            Multiselect = false
        };

        bool? result = openDlg.ShowDialog() == DialogResult.OK;
        if (result != true) return;

        FSystem = XMLParsers.ParseByXDocument(openDlg.FileName);
    }

该应用程序基本上允许用户更改不同的对象(FUser,FZone,FSource等)。我的想法是,用户将加载XML,然后能够在不同的 View 上编辑不同的列表对象。

在MVVM中执行此操作的正确方法是什么?

我计划(希望)获取“用户”,“区域”和“源” View 以显示Datagrid,这些数据网格已填充有来自模型的各自数据。

最佳答案

创建您特定的 View 模型,并使用依赖注入(inject)将相关数据传递给它们(此列表或该列表)。

这样, View 模型不需要了解其他内容,并且您可以轻松地对其进行模拟以进行测试和在设计器中查看虚拟数据。

最简单的示例是将粘贴复制到Linqpad中。两个模拟 View 模型都具有依赖关系(在我们的例子中为i)。您可以通过列表:

void Main()
{
    int someInt = 5;
    int anotherInt = 7;

    VM vm1 = new VM(someInt);
    VM vm2 = new VM(anotherInt);

    vm1.RevealI();
    vm2.RevealI();
}

public class VM{
    private int _i;

    public VM(int i)
    {
        _i = i;
    }

    public void RevealI() { Console.WriteLine("value of i is: " + _i); }
}

除此之外,还有更多项目:
  • MSDN
  • Code Project
  • stack overflow
  • 关于c# - 在MVVM中的 View 和ViewModel之间共享对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39367164/

    10-10 23:27
    查看更多