Xamarin.Forms:订阅事件处理程序VS.司令

时间:2016-03-24 15:25:21

标签: c# memory-management xamarin event-handling xamarin.forms

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()

1 个答案:

答案 0 :(得分:5)

通常,您的命令将作为视图模型的属性(在MVVM设计模式中调用)存在。它们封装了在视图模型上执行隔离操作的概念,或者从一个视图模型转换到另一个视图模型 - 例如在导航活动期间。这会将操作与可视界面分离,从而实现对该代码的单元测试。此外,由于命令是通过绑定在MVM中连接的,因此您不必担心取消订阅事件。

简短形式:

  • 命令通常是ViewModel的属性,与上面代码所示的不同。
  • 可通过编码单元测试进行测试。
  • 它们通过Binding语法而不是事件处理程序附加到可视元素。
  • 您不必担心取消订阅它们以进行内存管理 - 绑定不会固定与之关联的视图。

这更像是你的第二个例子:

public class ViewModel : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    private string _text;
    public string Text {
        get { return _text; }
        private set {
            if (_text != value ) {
                _text = value;
                OnPropertyChanged("Text");
            }
        }
    }
    public ICommand Cmd { get; private set; }
    public ViewModel() {
        Text = "Press me";
        Cmd = new Command(() => {
            System.Diagnostics.Debug.WriteLine("Pressed!");
            Text = "Thanks!";
        });
    }
    private void OnPropertyChanged(string propertyName) {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

//.....

public App ()
{
    Button button = new Button();
    button.BindingContext = new ViewModel();
    button.SetBinding(Button.TextProperty, "Text");
    button.SetBinding(Button.CommandProperty, "Cmd");

    // The root page of your application
    MainPage = new ContentPage {
        Content = new StackLayout {
            VerticalOptions = LayoutOptions.Center,
            Children = {
                button,
            }
        }
    };
}

请注意,ViewModel类包含零代码,用于处理可视控件,因此可以非常轻松地进行单元测试。此外,App类中处理UI的代码现在变得更加简单。

通常我建议使用XAML标记而不是代码。在我看来,Binding语法在XAML中更容易理解。

相关问题