0

I´ve a NavigationWindow with some pages. I navigate from one to another with buttons, and go back function of navigation window. My problem is I use a descriptor in some of the pages when they load, and I´d like to dispose it when you use go back function in navigationwindow (in fact the "descriptor" is Kinect, and when the page loads, it starts Kinect with sensor.start(), and I want to stop it when going back, sensor.stop()... but I think it´s the same as a file descriptor for this issue and much more people has worked with file descriptors).

Is there any way to extend the GoBack function in the page to dispose descriptors (in my code I only need to call sensor.stop(); )?

Thanks in advance

Alberto
  • 704
  • 2
  • 12
  • 26
  • If i understand what you are after correctly, my approach will be: handling descriptors disposal in OnNavigatedTo event of the Page. – har07 Dec 11 '13 at 01:26
  • Thanks. I´ll try OnNavigated, but in Page.OnNavigatedFrom, because I have the reference of the descriptor in the page that becomes inactive. – Alberto Dec 11 '13 at 01:48
  • I tried, but WPF doesn´t implement Page.OnNavigatedFrom :( – Alberto Dec 12 '13 at 23:34
  • yes you are right, therefore i suggested another approach in my answer. But anyway, nice to know you found your own approach. Will be useful for others with similar problem. – har07 Dec 13 '13 at 02:41
  • Your approach was very similar like my approach: using a Navigating event. But in my approach you use the event in the own object/page, so you have direct access to all the object properties. If you call it from another object, you have to allow access to the properties you want to dispose: you have to make them public (public Kinect sensor; instead of private KinectSensor sensor;) or implement a method like public void CloseKinect(). Anyway, your answer was very useful, and I marked as useful :) – Alberto Dec 13 '13 at 17:32

2 Answers2

1

My suggestion in the comment was based on windows phone development experience.. but after i tried applying that solution in wpf using navigationwindow, i found nothing like OnNavigatedTo/OnNavigatedFrom in WP/silverlight.

But i found Navigating event of NaviagtionWindow can be used instead. In that event, you can get this.CurrentSource which is Page2 (if you navigate back from Page2 to Page1) and dispose descriptors in that Page.

Hope this work.

har07
  • 88,338
  • 12
  • 84
  • 137
0

Ok, I found how to do a workaround. It also applies to the question: how to dispose an object in WPF. It´s weird all posts about dispose objects in WPF talk about GC and that you can´t dispose it yourself. Yes, GC dispose objects automatically, but when he wants. But maybe you want to dispose inmediately, or you have an object that needs previous operations before dispose. In my case, Kinect needs to be stopped before dispose (you can dispose without stopping, but kinect ir sensor is still working). And GC is not solution because I need to stop it before dispose.

So, the solution:

public partial class MyClass : Page
{
    private KinectSensor sensor;

    public MyClass()
    {
        InitializeComponent();
        this.Loaded += (s, e) =>  { NavigationService.Navigating += NavigationService_Navigating; };

        // What you want to add to the constructor
        // I want to start Kinect
        sensor = KinectSensor.KinectSensors.FirstOrDefault(k => k.Status == KinectStatus.Connected);
        sensor.Start();

    }

    public void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Back)
        {
            // What you want to do.
            // I want to stop and dispose Kinect
            if (sensor != null)
            {
                sensor.Stop();
                sensor.Dispose();
            }
        }
    }
}
Alberto
  • 704
  • 2
  • 12
  • 26