Prism从Model更新Viewmodel

时间:2016-11-01 05:33:52

标签: c# mvvm prism-6

我认为我的问题不正确,因此有一些不好的代码。 我想要做的是有更多的模型控制器(?) - ViewModel-View我在控制器中我可以拥有在多个ViewModel中使用的代码,所以我不必多次编写它。我无法掌握的部分是如何在Controller类的循环中更新ViewModel。控制器有一个循环计数器,我想用它来更新ViewModel中的参数,但是在我返回ViewModel之前没有更新。

我试图弄清楚如何使用Prism从模型更新Viewmodel。我可能会错误但我认为我可以使我的类成为一个可绑定的基础,然后让它在修改后自动更新我的viewmodel而不是使用INotifyPropertyChanged。

我有一个循环浏览某些目录并跟踪某些文件的方法。我想让循环更新一个实时更新viewmodel的字段,因为我使用BindableBase我相信我应该能够使用它来通知ViewModel有关模型的更新。现在,代码不会自动更新ViewModel,因为我对如何完成此任务感到有点迷失。

视图模型

public class P4LabelBatteryViewModel : BindableBase
    {
        private P4LabelBatteryModel p4LabelBatteryModel = new P4LabelBatteryModel();
        private ScriptsToTagModel scriptsToTagModel = new ScriptsToTagModel();
        private P4LabelBattery.Models.BatteryLocation batteryLocation = new BatteryLocation();


        public P4LabelBatteryViewModel()
        {
           P4LabelBattery.Models.BatteryLocation  p4LabelBattery = new BatteryLocation();

           // FilesProcessedBlck = p4LabelBattery.TestItem;


            this.GetBatteryBtn = new DelegateCommand(chooseFile, canChooseFile);
            this.NewLabelBtn = new DelegateCommand(chooseNewLabel, canNewLabel).ObservesProperty(() => NewLabel);
        }


        private string _batteryLocation = "Enter Location Here";
        public string BatteryLocation
        {
            get
            {
                return _batteryLocation;
            }
            set
            {
                SetProperty(ref _batteryLocation, value);
            }
        }

        public ICommand GetBatteryBtn
        {
            get;
            set;
        }


        private void chooseFile()
        {
            BatteriesToLoad = p4LabelBatteryModel.GetBatteryFileName();

            var b = BatteriesToLoad.Count;
            if (b != 0)
            {
                ScriptCollection = scriptsToTagModel.ReadInScrpts(BatteriesToLoad[b-1].BatteryLocationAdd);
                BatteryLocation = BatteriesToLoad[b - 1].BatteryLocationAdd;
            }
        }

        private bool canChooseFile()
        {
            if (BatteriesToLoad != null)
            {
                return true;
            }
            else
            {
                return true;
            }
        }


        public ICommand NewLabelBtn
        {
            get;
            set;
        }

        private void chooseNewLabel()
        {
            if (ScriptCollection.Count > 0)
            {
                ScriptCollection = P4LabelBatteryModel.LocatePNGModel(NewLabel, ScriptCollection, batteryLocation);
//                FilesProcessedBlck = ScriptCollection.Count;
                ScriptCollection = P4LabelBatteryModel.TagsFilesModel(NewLabel, ScriptCollection, FilesProcessedBlck);
            }
        }

        private bool canNewLabel()
        {
            if (NewLabel == null || NewLabel == "Enter Label Here" || NewLabel == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private string _newLabel;
        public string NewLabel
        {
            get
            {
               return _newLabel;
            }
            set
            {
                SetProperty(ref _newLabel, value);
                canNewLabel();
            }
        }

        private int _filesProcessedBlck;
        public int FilesProcessedBlck
        {
            get
            {
                return _filesProcessedBlck;
            }
            set
            {
                SetProperty(ref _filesProcessedBlck, batteryLocation.TestItem);
            }

        }


        private ObservableCollection<ScriptModel> _scriptCollection = new ObservableCollection<ScriptModel>();
        public ObservableCollection<ScriptModel> ScriptCollection
        {
            get
            {
                return _scriptCollection;
            }
            set
            {
                SetProperty(ref _scriptCollection, value);
            }

        }

        private ObservableCollection<BatteryLocation> _batteriesToLoad = new ObservableCollection<BatteryLocation>();
        public ObservableCollection<BatteryLocation> BatteriesToLoad 
        {
            get
            {
                return _batteriesToLoad;
            }
            set
            {
                SetProperty(ref _batteriesToLoad, value);
            }

        }



    }

型号:

class P4LabelBatteryModel : BindableBase
    {
        private string _MasterFile2 = "0";
        private ObservableCollection<BatteryLocation> batteryLocationColl = new ObservableCollection<BatteryLocation>();

        public ObservableCollection<BatteryLocation> GetBatteryFileName()
        {
            if (_MasterFile2 != "1")
            {
                Microsoft.Win32.OpenFileDialog _MasterFileDialog = new Microsoft.Win32.OpenFileDialog();

                _MasterFileDialog.DefaultExt = "*.*";
             //   _MasterFileDialog.Filter = "All Files (*.scr; *.SCR) | *.scr; *.SR";

                Nullable<bool> result = _MasterFileDialog.ShowDialog();

                BatteryLocation batteryLocation = new BatteryLocation();
                batteryLocation.BatteryLocationAdd =  _MasterFileDialog.FileName;

                if (result == true && !batteryLocationColl.Contains(batteryLocation))
                {
                    batteryLocation.BatteryLocationAdd = _MasterFileDialog.FileName;
                    batteryLocationColl.Add(batteryLocation);
                }
            }
                return batteryLocationColl;
        }

        public static ObservableCollection<ScriptModel> LocatePNGModel(string NewLabel, ObservableCollection<ScriptModel> observableCollection, BatteryLocation FilesProcessedBlck)
        {
            string newLabel = NewLabel;
            var scriptsToTagColl = observableCollection;
            string[] files = null;
            FilesProcessedBlck.TestItem = 40;

            // ObservableCollection<ScriptModel> newCollection = new ObservableCollection<ScriptModel>();

            int CollCount = scriptsToTagColl.Count;
            for (int i = 0; i < CollCount; i++)
            {

                //Check for 4th occurance of \ to search for .png files.
                // Modify path for non-standard script locations.
                if (!scriptsToTagColl[i].ScriptNameAdd.Contains("Scripts"))
                {
                    int j = 0;
                    int m = 0;

                    for (int k = 0; k <= 4 && k < scriptsToTagColl[i].ScriptNameAdd.Count(); k++)
                    {
                        if (j != -1)
                        {
                            if ((j = scriptsToTagColl[i].ScriptNameAdd.IndexOf(@"\", j)) != -1 && k == 4)
                            {
                                //add directory search for .png
                                string teststring = scriptsToTagColl[i].ScriptNameAdd;
                                int testint = teststring.Count();
                                int ttt = testint;
                                string _edtdString = teststring.Remove(j, testint - j);
                                try
                                {
                                    foreach (string d in Directory.GetDirectories(_edtdString))
                                    {
                                        try
                                        {

                                            files = Directory.GetFiles(d, "*.png", SearchOption.AllDirectories);

                                            foreach (string file1 in files)
                                            {
                                                ScriptModel _scriptModel = new ScriptModel();
                                                _scriptModel.ScriptNameAdd = file1;
                                                scriptsToTagColl.Add(_scriptModel);
                                              //  newCollection.Add(_scriptModel);
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            //don't care right now.
                                            //files = null;
                                            //testscript2 = testscript.Insert(index + @"Scripts\".Length, @"Results\");
                                            //trimmed = testscript.TrimEnd('.', 's', 'c', 'r');
                                            //trimmed += @"\mill\Snapshots\BCM";
                                            int stophere = 2;


                                        }

                                    }
                                }
                                catch (Exception e)
                                {
                                    //don't care right now
                                    int stophere2 = 2;
                                }

                                break;
                            }
                            j++;
                        }
                    }
                }
                //End of check
                else
                {
                    string testscript = scriptsToTagColl[i].ScriptNameAdd;
                    int index = testscript.IndexOf(@"Scripts\");
                    files = null;
                    string testscript2 = testscript.Insert(index + @"Scripts\".Length, @"Results\");
                    string trimmed = testscript2.TrimEnd('.', 's', 'c', 'r');
                    trimmed += @"\mill\Snapshots\BCM";

                    if (Directory.Exists(@trimmed))
                    {
                        try
                        {

                            files = Directory.GetFiles(@trimmed, "*.*");

                            foreach (string file1 in files)
                            {
                                ScriptModel _scriptModel = new ScriptModel();
                                _scriptModel.ScriptNameAdd = file1;
                          //      newCollection.Add(_scriptModel);
                                scriptsToTagColl.Add(_scriptModel);
                            }
                        }
                        catch (Exception e)
                        {
                            //don't care right now.
                            files = null;
                            testscript2 = testscript.Insert(index + @"Scripts\".Length, @"Results\");
                            trimmed = testscript.TrimEnd('.', 's', 'c', 'r');
                            trimmed += @"\mill\Snapshots\BCM";


                        }
                    }
                }

            }

            return scriptsToTagColl;
        }

        public static ObservableCollection<ScriptModel> TagsFilesModel(string NewLabel, IList<ScriptModel> observableCollection, int FileCount)
        {
            string newLabel = NewLabel;
            var scriptsToTagColl = observableCollection;
            int fileCount = FileCount;

            var _p4LabelBatteryViewModel = new P4LabelBatteryViewModel();

            //This will generate an IPC when returned
            ObservableCollection<ScriptModel> newCollection = new ObservableCollection<ScriptModel>();

            int CollCount = scriptsToTagColl.Count;

            for (int i = 0; i < scriptsToTagColl.Count; i++)
            {
                var stringToTest = scriptsToTagColl[i].ScriptNameAdd;
               string tempString = @" """;

              //  if (stringToTest.Contains(" "))
              //  {
                    tempString = tempString + stringToTest;
                    stringToTest = " " + tempString;
             //   }


                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                StringBuilder q = new StringBuilder();

                string FileName1 = @"c:\windows\system32\cmd.exe";
                string Arguments1 = string.Format("@ /c p4 tag -l {0} {1}", newLabel, stringToTest);

               proc.StartInfo = new ProcessStartInfo(FileName1, Arguments1);
               proc.StartInfo.RedirectStandardError = true;
               proc.StartInfo.UseShellExecute = false;
               proc.StartInfo.CreateNoWindow = true;
               proc.Start();

               q.Append(proc.StandardError.ReadToEnd());
                //while (!proc.HasExited)
                //{
                //    q.Append(proc.StandardOutput.ReadToEnd());

                //}
                string r = q.ToString();  

                if ( ! r.Contains("no such file(s)"))
                {
                scriptsToTagColl[i].BackGround = new SolidColorBrush(Colors.LightGreen);
                }

                newCollection.Add(scriptsToTagColl[i]);
            }

            return newCollection;
        }
    }

    public class BatteryLocation :BindableBase
    {
        private string _batteryLocationAdd;

        public string BatteryLocationAdd
        {
            get { return _batteryLocationAdd; }
            set { _batteryLocationAdd = value; }
        }

        private SolidColorBrush _backGround;
        public SolidColorBrush BackGround
        {
            get { return _backGround; }
            set { _backGround = value; }
        }

        private int _testItem = 100;
        public int TestItem
        {
            get {return _testItem;}
            set { _testItem = value; }

        }

    }

}

0 个答案:

没有答案