繁体   English   中英

ReactiveUI OneWayBind泄漏句柄

[英]ReactiveUI OneWayBind leaks handles

我有一个简单的ViewModel:

public class MeetingPageViewModel : ReactiveObject, IRoutableViewModel
{
    public MeetingPageViewModel(IScreen hs, IMeetingRef mRef)
    {
        HostScreen = hs;
        _backing = "hi there";
    }

    public IScreen HostScreen { get; private set; }

    public string MeetingTitle
    {
        get { return _backing; }
        set { this.RaiseAndSetIfChanged(ref _backing, value); }
    }
    string _backing;

    public string UrlPathSegment
    {
        get { return "/meeting"; }
    }
}

我绑定到MeetingTitle一个TextBlock:

public sealed partial class MeetingPage : Page, IViewFor<MeetingPageViewModel>
{
    public MeetingPage()
    {
        this.InitializeComponent();

        // Bind everything together we need.
        this.OneWayBind(ViewModel, x => x.MeetingTitle, y => y.MeetingTitle.Text);
    }

    /// <summary>
    /// Stash the view model
    /// </summary>
    public MeetingPageViewModel ViewModel
    {
        get { return (MeetingPageViewModel)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }
    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel", typeof(MeetingPageViewModel), typeof(MeetingPage), new PropertyMetadata(null));

    object IViewFor.ViewModel
    {
        get { return ViewModel; }
        set { ViewModel = (MeetingPageViewModel)value; }
    }
}

导航回到上一个屏幕后,MeetingPageViewModel不会被垃圾回收。 我正在使用RxUI和VS2013的6.4.1(以及内存分析工具)。 如果我处理掉OneWayBind的返回值,则所有内容都会被正确清理-但是,当然,我不再拥有绑定了。

原来问题是DependencyProperty ViewModel。 它的生命周期是永远的(请注意其声明中的“静态”)。 结果,附加到它的绑定永远不会被垃圾回收,因为它永远不会消失,然后该绑定同时拥有对视图和视图模型的引用,因此它们永远不会消失。

打破此问题的唯一方法是显式清理绑定。 RxUI提供了WhenActivated方法来帮助解决此问题。 将绑定包围在lambda中,并使用提供的功能跟踪IDisposals。 当视图消失时,将对其进行清理。

this.WhenActivated(disposeOfMe => {
    disposeOfMe (this.OneWayBind(ViewModel, x => x.MeetingTitle, y => y.MeetingTitle.Text));
});

我知道这是一个非常老的问题,但是...

_backing = "hi there";

不应该这样写:

MeetingTitle = "hi there" ;

因为这样可以通知视图有关属性的更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM