我是wpf和mvvm概念的新手。這裏有一個tutorial我正在學習,但我不明白這部分; 在圖7:在這段代碼中做什麼+ =和 - =?
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
// Create the ViewModel to which
// the main window binds.
string path = "Data/customers.xml";
var viewModel = new MainWindowViewModel(path);
// When the ViewModel asks to be closed,
// close the window.
EventHandler handler = null;
handler = delegate
{
viewModel.RequestClose -= handler;
window.Close();
};
viewModel.RequestClose += handler;
// Allow all controls in the window to
// bind to the ViewModel by setting the
// DataContext, which propagates down
// the element tree.
window.DataContext = viewModel;
window.Show();
}
什麼是viewModel.RequestClose -= handler;
和viewModel.RequestClose += handler;
在做什麼?
具體而言,+ =基本上意味着,保持左側的對象的原始內容,並追加右側的事情, - =表示保持事物的內容在左邊,除了右邊的東西,它是var1 = var1 + var2的快捷方式。 –
嗨,謝謝。但我無法理解代碼。你能解釋一下嗎?什麼是RequestClose以及爲什麼它向其添加事件處理程序? – Saeid
@AldenBe:我已經知道了。但我不明白它如何與事件處理程序一起工作。 – Saeid