How can I remove / garbage collect an attached method from a tabbarController.ViewControllerSelected event?












2














I have this code that attaches: OnTabbarControllerItemSelected; to tabbarController.ViewControllerSelected. From what I understand events like this that are attached should also be detached later.



Can someone give me advice on where I should do the detach. Also should I override the Dispose method for this code and if so how should I do that?



Code:



    protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);

if (e.NewElement != null)
{
_page = (MainPage)e.NewElement;
}
else
{
_page = (MainPage)e.OldElement;
}

if (e.OldElement != null)
{
Xamarin.Forms.Application.Current.PropertyChanged -= Current_PropertyChanged;
return;
}

try
{
var tabbarController = (UITabBarController)this.ViewController;

if (null != tabbarController)
{
Xamarin.Forms.Application.Current.PropertyChanged += Current_PropertyChanged; //subscribe to the App class' built in property changed event
tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;

UpdateTheme();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}


Would appreciate some advice on how I can detach this method in a similar way to the way that the Current_PropertyChanged method is being detached.










share|improve this question
























  • Do you have multiple UITabBarControllers? Do you need to subscribe to a ViewControllerSelected event each time an ElementChanged event occurs?
    – dymanoid
    Nov 12 '18 at 17:27


















2














I have this code that attaches: OnTabbarControllerItemSelected; to tabbarController.ViewControllerSelected. From what I understand events like this that are attached should also be detached later.



Can someone give me advice on where I should do the detach. Also should I override the Dispose method for this code and if so how should I do that?



Code:



    protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);

if (e.NewElement != null)
{
_page = (MainPage)e.NewElement;
}
else
{
_page = (MainPage)e.OldElement;
}

if (e.OldElement != null)
{
Xamarin.Forms.Application.Current.PropertyChanged -= Current_PropertyChanged;
return;
}

try
{
var tabbarController = (UITabBarController)this.ViewController;

if (null != tabbarController)
{
Xamarin.Forms.Application.Current.PropertyChanged += Current_PropertyChanged; //subscribe to the App class' built in property changed event
tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;

UpdateTheme();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}


Would appreciate some advice on how I can detach this method in a similar way to the way that the Current_PropertyChanged method is being detached.










share|improve this question
























  • Do you have multiple UITabBarControllers? Do you need to subscribe to a ViewControllerSelected event each time an ElementChanged event occurs?
    – dymanoid
    Nov 12 '18 at 17:27
















2












2








2







I have this code that attaches: OnTabbarControllerItemSelected; to tabbarController.ViewControllerSelected. From what I understand events like this that are attached should also be detached later.



Can someone give me advice on where I should do the detach. Also should I override the Dispose method for this code and if so how should I do that?



Code:



    protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);

if (e.NewElement != null)
{
_page = (MainPage)e.NewElement;
}
else
{
_page = (MainPage)e.OldElement;
}

if (e.OldElement != null)
{
Xamarin.Forms.Application.Current.PropertyChanged -= Current_PropertyChanged;
return;
}

try
{
var tabbarController = (UITabBarController)this.ViewController;

if (null != tabbarController)
{
Xamarin.Forms.Application.Current.PropertyChanged += Current_PropertyChanged; //subscribe to the App class' built in property changed event
tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;

UpdateTheme();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}


Would appreciate some advice on how I can detach this method in a similar way to the way that the Current_PropertyChanged method is being detached.










share|improve this question















I have this code that attaches: OnTabbarControllerItemSelected; to tabbarController.ViewControllerSelected. From what I understand events like this that are attached should also be detached later.



Can someone give me advice on where I should do the detach. Also should I override the Dispose method for this code and if so how should I do that?



Code:



    protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);

if (e.NewElement != null)
{
_page = (MainPage)e.NewElement;
}
else
{
_page = (MainPage)e.OldElement;
}

if (e.OldElement != null)
{
Xamarin.Forms.Application.Current.PropertyChanged -= Current_PropertyChanged;
return;
}

try
{
var tabbarController = (UITabBarController)this.ViewController;

if (null != tabbarController)
{
Xamarin.Forms.Application.Current.PropertyChanged += Current_PropertyChanged; //subscribe to the App class' built in property changed event
tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;

UpdateTheme();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}


Would appreciate some advice on how I can detach this method in a similar way to the way that the Current_PropertyChanged method is being detached.







c# xamarin xamarin.forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '18 at 15:15









Brandon Minnick

6,110122871




6,110122871










asked Oct 27 '18 at 10:51









Alan2

1,48354132252




1,48354132252












  • Do you have multiple UITabBarControllers? Do you need to subscribe to a ViewControllerSelected event each time an ElementChanged event occurs?
    – dymanoid
    Nov 12 '18 at 17:27




















  • Do you have multiple UITabBarControllers? Do you need to subscribe to a ViewControllerSelected event each time an ElementChanged event occurs?
    – dymanoid
    Nov 12 '18 at 17:27


















Do you have multiple UITabBarControllers? Do you need to subscribe to a ViewControllerSelected event each time an ElementChanged event occurs?
– dymanoid
Nov 12 '18 at 17:27






Do you have multiple UITabBarControllers? Do you need to subscribe to a ViewControllerSelected event each time an ElementChanged event occurs?
– dymanoid
Nov 12 '18 at 17:27














2 Answers
2






active

oldest

votes


















3














Have you tried tabbarController.ViewControllerSelected -= OnTabbarControllerItemSelected;?






share|improve this answer





















  • Where would this be placed. Can you give the full code example in your answer please. Thanks
    – Alan2
    Oct 30 '18 at 4:58










  • I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
    – Jack Hua - MSFT
    Oct 30 '18 at 5:13





















0














From my understanding, event handler is just a delegate, a normal reference-type object under the control of GC. It doesn't need to be manually detached if it remains valid until the GC collects the event itself. This should be true in most cases (e.g. a handler inside the same window).



It's possible that you need to detach manually. That happens when a child object is listening to an event of parent object. If the child object could be Disposed before the parent, the handler becomes invalid (as it might refer to the disposed child object). In this case, you should always know when it becomes invalid (in the above case, the Dispose of the child object), and that's when and where you should detach it.



EDIT
From the code you provided it's not easy to tell but it's more likely you don't need to detach OnTabbarControllerItemSelected. But you probably need to be careful to Xamarin.Forms.Application.Current.PropertyChanged.



Also see this question.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53021142%2fhow-can-i-remove-garbage-collect-an-attached-method-from-a-tabbarcontroller-vi%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Have you tried tabbarController.ViewControllerSelected -= OnTabbarControllerItemSelected;?






    share|improve this answer





















    • Where would this be placed. Can you give the full code example in your answer please. Thanks
      – Alan2
      Oct 30 '18 at 4:58










    • I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
      – Jack Hua - MSFT
      Oct 30 '18 at 5:13


















    3














    Have you tried tabbarController.ViewControllerSelected -= OnTabbarControllerItemSelected;?






    share|improve this answer





















    • Where would this be placed. Can you give the full code example in your answer please. Thanks
      – Alan2
      Oct 30 '18 at 4:58










    • I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
      – Jack Hua - MSFT
      Oct 30 '18 at 5:13
















    3












    3








    3






    Have you tried tabbarController.ViewControllerSelected -= OnTabbarControllerItemSelected;?






    share|improve this answer












    Have you tried tabbarController.ViewControllerSelected -= OnTabbarControllerItemSelected;?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 29 '18 at 8:14









    Jack Hua - MSFT

    6477




    6477












    • Where would this be placed. Can you give the full code example in your answer please. Thanks
      – Alan2
      Oct 30 '18 at 4:58










    • I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
      – Jack Hua - MSFT
      Oct 30 '18 at 5:13




















    • Where would this be placed. Can you give the full code example in your answer please. Thanks
      – Alan2
      Oct 30 '18 at 4:58










    • I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
      – Jack Hua - MSFT
      Oct 30 '18 at 5:13


















    Where would this be placed. Can you give the full code example in your answer please. Thanks
    – Alan2
    Oct 30 '18 at 4:58




    Where would this be placed. Can you give the full code example in your answer please. Thanks
    – Alan2
    Oct 30 '18 at 4:58












    I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
    – Jack Hua - MSFT
    Oct 30 '18 at 5:13






    I think the place where would be placed depending on where you what to detach it.It's just a way to detach this method.
    – Jack Hua - MSFT
    Oct 30 '18 at 5:13















    0














    From my understanding, event handler is just a delegate, a normal reference-type object under the control of GC. It doesn't need to be manually detached if it remains valid until the GC collects the event itself. This should be true in most cases (e.g. a handler inside the same window).



    It's possible that you need to detach manually. That happens when a child object is listening to an event of parent object. If the child object could be Disposed before the parent, the handler becomes invalid (as it might refer to the disposed child object). In this case, you should always know when it becomes invalid (in the above case, the Dispose of the child object), and that's when and where you should detach it.



    EDIT
    From the code you provided it's not easy to tell but it's more likely you don't need to detach OnTabbarControllerItemSelected. But you probably need to be careful to Xamarin.Forms.Application.Current.PropertyChanged.



    Also see this question.






    share|improve this answer


























      0














      From my understanding, event handler is just a delegate, a normal reference-type object under the control of GC. It doesn't need to be manually detached if it remains valid until the GC collects the event itself. This should be true in most cases (e.g. a handler inside the same window).



      It's possible that you need to detach manually. That happens when a child object is listening to an event of parent object. If the child object could be Disposed before the parent, the handler becomes invalid (as it might refer to the disposed child object). In this case, you should always know when it becomes invalid (in the above case, the Dispose of the child object), and that's when and where you should detach it.



      EDIT
      From the code you provided it's not easy to tell but it's more likely you don't need to detach OnTabbarControllerItemSelected. But you probably need to be careful to Xamarin.Forms.Application.Current.PropertyChanged.



      Also see this question.






      share|improve this answer
























        0












        0








        0






        From my understanding, event handler is just a delegate, a normal reference-type object under the control of GC. It doesn't need to be manually detached if it remains valid until the GC collects the event itself. This should be true in most cases (e.g. a handler inside the same window).



        It's possible that you need to detach manually. That happens when a child object is listening to an event of parent object. If the child object could be Disposed before the parent, the handler becomes invalid (as it might refer to the disposed child object). In this case, you should always know when it becomes invalid (in the above case, the Dispose of the child object), and that's when and where you should detach it.



        EDIT
        From the code you provided it's not easy to tell but it's more likely you don't need to detach OnTabbarControllerItemSelected. But you probably need to be careful to Xamarin.Forms.Application.Current.PropertyChanged.



        Also see this question.






        share|improve this answer












        From my understanding, event handler is just a delegate, a normal reference-type object under the control of GC. It doesn't need to be manually detached if it remains valid until the GC collects the event itself. This should be true in most cases (e.g. a handler inside the same window).



        It's possible that you need to detach manually. That happens when a child object is listening to an event of parent object. If the child object could be Disposed before the parent, the handler becomes invalid (as it might refer to the disposed child object). In this case, you should always know when it becomes invalid (in the above case, the Dispose of the child object), and that's when and where you should detach it.



        EDIT
        From the code you provided it's not easy to tell but it's more likely you don't need to detach OnTabbarControllerItemSelected. But you probably need to be careful to Xamarin.Forms.Application.Current.PropertyChanged.



        Also see this question.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 2:08









        Wu Zhenwei

        1469




        1469






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53021142%2fhow-can-i-remove-garbage-collect-an-attached-method-from-a-tabbarcontroller-vi%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Coverage of Google Street View

            Full-time equivalent

            Surfing