我是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());
}
}