加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 何时何地调用RemoveObserver

发布时间:2020-12-15 20:59:42 所属栏目:百科 来源:网络整理
导读:我有一个UITextView子类,我添加了一个NSNotificationCenter观察器.但是我又在哪里删除了观察者呢? 我的代码: _textDidChangeNotification = UITextView.Notifications.ObserveTextDidChange(TextDidChange); 在Objective C中,我会在dealloc方法中执行此操
我有一个UITextView子类,我添加了一个NSNotificationCenter观察器.但是我又在哪里删除了观察者呢?

我的代码:

_textDidChangeNotification = UITextView.Notifications.ObserveTextDidChange(TextDidChange);

在Objective C中,我会在dealloc方法中执行此操作,但我不确定在C#中的相同位置

据我所知,我应该打电话给我

_textDidChangeNotification.Dispose()

我曾尝试过

protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (disposing)
        {
            _textDidChangeNotification.Dispose();
        }
    }

但它永远不会被称为.

完整的课程,按要求:

public class PlaceholderTextView : UITextView
{
    public string Placeholder 
    { 
        get { return PlaceholderLabel.Text; }
        set
        { 
            PlaceholderLabel.Text = value; 
            PlaceholderLabel.SizeToFit();
        }
    }

    protected UILabel PlaceholderLabel { get; set; }

    protected NSObject _textDidChangeNotification;

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
            AdjustPlaceholderHidden();
        }
    }

    public PlaceholderTextView() 
    {
        SetupLayout();

        _textDidChangeNotification
        = UITextView.Notifications.ObserveTextDidChange(TextDidChange);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        _textDidChangeNotification.Dispose();
    }

    protected void SetupLayout()
    {
        PlaceholderLabel = new UILabel(new CGRect(0,9,0));
        PlaceholderLabel.TextColor = UIColor.FromWhiteAlpha(0.702f,1f);

        AddSubview(PlaceholderLabel);
    }

    protected void AdjustPlaceholderHidden()
    {
        if (Text.Length > 0)
        {
            PlaceholderLabel.Hidden = true;
        }
        else
        {
            PlaceholderLabel.Hidden = false;
        }
    }

    protected void TextDidChange(object sender,Foundation.NSNotificationEventArgs args)
    {
        AdjustPlaceholderHidden();
    }       
}

解决方法

我会在ViewWillDisappear中这样做:

public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear (animated);

        SubscribeMessages ();
    }

    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);
        UnSubscribeMessages ();
    }

    public void SubscribeMessages ()
    {
        _hideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification,OnKeyboardNotification);
        _showObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification,OnKeyboardNotification);
    }

    public void UnSubscribeMessages ()
    {
        if (_hideObserver != null) NSNotificationCenter.DefaultCenter.RemoveObserver (_hideObserver);
        if (_showObserver != null) NSNotificationCenter.DefaultCenter.RemoveObserver(_showObserver);
    }

或者像Xamarin示例代码here中的ViewDidDis似

更新
我明白你的意思了,我怀疑某些事情阻止了自定义视图被垃圾收集.你有没有看过这个blog post可能会有所帮助.

同样从这个sample code看起来你正在调用dispose但是它们在ViewDidUnload here上清空了自定义视图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读