在哪里取消订阅Xamarin.Forms条目中的活动



我有一个从Xamarin.Forms entry继承的自定义条目,用于添加CompletedCommand属性。为此,我在构造函数中订阅Xamarin.Forms条目的Completed Event,并调用可绑定的Property命令。

我的问题是,有必要取消订阅吗?如果是这样的话,哪里是做这件事的好地方?

public class Entry : Xamarin.Forms.Entry
{
public static readonly BindableProperty CompletedCommandProperty = BindableProperty.Create(
propertyName: nameof(CompletedCommand),
returnType: typeof(ICommand),
declaringType: typeof(Entry),
defaultValue: null,
defaultBindingMode: BindingMode.TwoWay);
/// <summary>
/// The Command to execute when this entry is completed (Enter pressed).
/// </summary>
public ICommand CompletedCommand
{
get => (ICommand)GetValue(CompletedCommandProperty);
set => SetValue(CompletedCommandProperty, value);
}
public Entry() : base()
{
Completed += Entry_Completed;
}
private void Entry_Completed(object sender, EventArgs e)
{
CompletedCommand?.Execute(CompletedCommandParameter);
}
}

谷歌搜索"xamarin取消订阅事件";给出了很多有趣的结果。我觉得,如果可能的话,你想使用EventToCommandBehavior(我相信是xamarin工具包的一部分?(来避免使用事件。但如果你不能做到这一点,取消订阅可能是不必要的,但仍然是一种最佳做法。

来源:https://github.com/xamarin/xamarin-forms-samples/issues/188

最新更新