Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 委托使用ViewModels中的MVVM灯光处理事件。_C#_Wpf_Mvvm - Fatal编程技术网

C# 委托使用ViewModels中的MVVM灯光处理事件。

C# 委托使用ViewModels中的MVVM灯光处理事件。,c#,wpf,mvvm,C#,Wpf,Mvvm,我是MVVM的新手。在视图的代码隐藏中,窗口初始化事件中有很多代码。它有代理来处理多个事件。我想在视图模型中考虑所有这些。当您使用MVVM Light实现MVVM时,是否可以让代理处理ViewModel中的事件?我试图掌握EventToCommand机制,但很难理解。有人可以使用这个代码示例来帮助我理解如何使用它吗 private void Window_Initialized(object sender, EventArgs e) { try { _Ap

我是MVVM的新手。在视图的代码隐藏中,窗口初始化事件中有很多代码。它有代理来处理多个事件。我想在视图模型中考虑所有这些。当您使用MVVM Light实现MVVM时,是否可以让代理处理ViewModel中的事件?我试图掌握EventToCommand机制,但很难理解。有人可以使用这个代码示例来帮助我理解如何使用它吗

private void Window_Initialized(object sender, EventArgs e)
  {
     try
     {
        _AppComment = new CommentHandler(App_Comment);
        _AppUnitComment = new UnitCommentHandler(App_UnitComment);
        _AppActualListChanged = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ActualList_CollectionChanged);

        DataContext = AppVerifier.Instance;
        AppVerifier.Instance.Comment += _AppComment;
        AppVerifier.Instance.UnitComment += _AppUnitComment;
        AppVerifier.Instance.ActualList.CollectionChanged += _AppActualListChanged;
        AppVerifier.Instance.PropertyChanged += App_PropertyChanged;
        AppVerifier.Instance.msgClient.PropertyChanged += App_ConnectivityChanged;
        AppVerifier.Instance.ActualListReceived += App_ActualListReceived;
        PopulateTestMenu();
        PopulateChangeMenu();

        _jumpToLog.Header = "View in Log...";
        Uri uri = new Uri(@"/App;component/Resources/log.png", UriKind.Relative);
        System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage(uri);
        Image imgIcon = new Image();
        imgIcon.Height = 16;
        imgIcon.Width = 16;
        imgIcon.Source = bitmap;
        _jumpToLog.Icon = imgIcon;
        _jumpToLog.Click += new RoutedEventHandler(JumpToLog_Click);
        _jumpToLog.ToolTip = "Shows the selected feature in the log window.";

        Binding b = new Binding();
        b.Source = listFeatures;
        b.Path = new PropertyPath("SelectedIndex");
        b.Converter = new EnabledConverter();

        menuReset.SetBinding(MenuItem.IsEnabledProperty, b);
        menuBarReset.SetBinding(MenuItem.IsEnabledProperty, b);

        listFeatures.SelectionChanged += new SelectionChangedEventHandler(listFeatures_SelectionChanged);

        MsgQueueWindow.Instance.MsgQueue.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MsgQueue_CollectionChanged);
        UnitMsgQueueWindow.Instance.MsgQueue.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(UnitMsgQueue_CollectionChanged);

        if (!Settings.Default.MainPos.IsEmpty)
        {
           Left = Settings.Default.MainPos.X;
           Top = Settings.Default.MainPos.Y;
        }

        if (!Settings.Default.MainSize.IsEmpty)
        {
           Height = Settings.Default.MainSize.Height;
           Width = Settings.Default.MainSize.Width;
        }

        AppVerifier.Instance.msgClient.RouterFinder.Connect();
        AppVerifier.Instance.msgClient.RouterFinder.Poll();

        Data.LogHandler.LogSettings(); //7121 Move Logs To AppDataFolder

        if (Settings.Default.AutoConnect)
        {
           System.Net.IPAddress ip;

           if (System.Net.IPAddress.TryParse(Settings.Default.Ip, out ip))
           {
              AppVerifier.Instance.Connect(ip, Settings.Default.Port, Settings.Default.Key);
           }
        }

        if (Settings.Default.AutoStartFileLog)
        {
           string fileName = DateTime.Now.ToString("yyyy-MM-dd HHmmss") + " ApplicationVerification.csv";
           string path = System.IO.Path.Combine(Settings.Default.AutoStartFileLogPath, fileName);
           Logger.Instance.StartFileLogging(path);
        }



     }
     catch (Exception ex)
     {
        ErrorLogger.Log(LogLevel.Error,ex.ToString());
     }                                                                                                                                                                                                                       
  }

欢迎来到堆栈溢出。你的问题似乎是重复的,所以我投票决定结束它。但是,如果您按照该问题的链接并看到公认的答案,您将找到问题的解决方案。简言之,当使用MVVM时,我们可以使用附加属性处理事件。发现本文非常有用。[链接]()