【发布时间】:2016-03-24 15:25:21
【问题描述】:
Commanding 主要用于在 ViewModel 和用户界面之间进行清晰的分离。事件订阅和commanding有什么区别?考虑以下示例:
public App ()
{
Button button = new Button
{
Text = "Press me",
};
button.Clicked += Button_Clicked;
// The root page of your application
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
button,
}
}
};
}
private void Button_Clicked(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Pressed!");
}
VS.
public App ()
{
Button button = new Button
{
Text = "Press me",
};
button.Command = new Command(() => System.Diagnostics.Debug.WriteLine("Pressed!"));
// The root page of your application
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
button,
}
}
};
}
关于内存管理,应该取消订阅该事件。这对指挥也有效吗?应该在哪里订阅/取消订阅该事件?在OnAppearing() 和OnDisappearing()?
【问题讨论】:
标签: c# memory-management xamarin event-handling xamarin.forms