Is it okay to have a MessageCenter Subscribe in a page constructor?











up vote
0
down vote

favorite












I'm subscribing to a MessageCenter message in a page constructor as I want it to change the page even before the page has appeared:



    public PhrasesFrame()
{
InitializeComponent();
vm = new PhrasesFrameViewModel();
BindingContext = vm;
vm.Theme = Settings.th.ToString();
MessagingCenter.Subscribe<SettingsPage>(this, "ThemeChanged", (sender) => {
vm.Theme = Settings.th.ToString();
});
}


The PhrasesFrame is only created one time in my application as it's one of the tabs.



Is there any issue with Subscribing here. The reason I ask is I would not have an UnSubscribe or at least I don't know where to put one.










share|improve this question
























  • stackoverflow.com/questions/29252351/… Why don't you subscribe in appearing and unsubscribe in disappearing?
    – Felix D.
    8 hours ago

















up vote
0
down vote

favorite












I'm subscribing to a MessageCenter message in a page constructor as I want it to change the page even before the page has appeared:



    public PhrasesFrame()
{
InitializeComponent();
vm = new PhrasesFrameViewModel();
BindingContext = vm;
vm.Theme = Settings.th.ToString();
MessagingCenter.Subscribe<SettingsPage>(this, "ThemeChanged", (sender) => {
vm.Theme = Settings.th.ToString();
});
}


The PhrasesFrame is only created one time in my application as it's one of the tabs.



Is there any issue with Subscribing here. The reason I ask is I would not have an UnSubscribe or at least I don't know where to put one.










share|improve this question
























  • stackoverflow.com/questions/29252351/… Why don't you subscribe in appearing and unsubscribe in disappearing?
    – Felix D.
    8 hours ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm subscribing to a MessageCenter message in a page constructor as I want it to change the page even before the page has appeared:



    public PhrasesFrame()
{
InitializeComponent();
vm = new PhrasesFrameViewModel();
BindingContext = vm;
vm.Theme = Settings.th.ToString();
MessagingCenter.Subscribe<SettingsPage>(this, "ThemeChanged", (sender) => {
vm.Theme = Settings.th.ToString();
});
}


The PhrasesFrame is only created one time in my application as it's one of the tabs.



Is there any issue with Subscribing here. The reason I ask is I would not have an UnSubscribe or at least I don't know where to put one.










share|improve this question















I'm subscribing to a MessageCenter message in a page constructor as I want it to change the page even before the page has appeared:



    public PhrasesFrame()
{
InitializeComponent();
vm = new PhrasesFrameViewModel();
BindingContext = vm;
vm.Theme = Settings.th.ToString();
MessagingCenter.Subscribe<SettingsPage>(this, "ThemeChanged", (sender) => {
vm.Theme = Settings.th.ToString();
});
}


The PhrasesFrame is only created one time in my application as it's one of the tabs.



Is there any issue with Subscribing here. The reason I ask is I would not have an UnSubscribe or at least I don't know where to put one.







xamarin xamarin.forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago









Rohit Verma

1,4372623




1,4372623










asked 8 hours ago









Alan2

1,45554131245




1,45554131245












  • stackoverflow.com/questions/29252351/… Why don't you subscribe in appearing and unsubscribe in disappearing?
    – Felix D.
    8 hours ago




















  • stackoverflow.com/questions/29252351/… Why don't you subscribe in appearing and unsubscribe in disappearing?
    – Felix D.
    8 hours ago


















stackoverflow.com/questions/29252351/… Why don't you subscribe in appearing and unsubscribe in disappearing?
– Felix D.
8 hours ago






stackoverflow.com/questions/29252351/… Why don't you subscribe in appearing and unsubscribe in disappearing?
– Felix D.
8 hours ago














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You're already answering your own question basically. You can, of course, subscribe here. But you will need to find a point in your page/app's lifecycle to unsubscribe. Else, this page might be alive forever, leaking memory, while that is not what you want.



A better option might be to subscribe whenever it appears and unsubscribe when it disappears as suggested in the comments. Is there any reason not to do that?



You could also subscribe in the constructor and unsubscribe in the disappearing, but then the subscribe event will never happen again if the page is only instantiated once.



Long answer short; you will want to unsubscribe. So as long as you can cover that and make it work with your requirements, subscribing in your constructor is fine.






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',
    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%2f53203315%2fis-it-okay-to-have-a-messagecenter-subscribe-in-a-page-constructor%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You're already answering your own question basically. You can, of course, subscribe here. But you will need to find a point in your page/app's lifecycle to unsubscribe. Else, this page might be alive forever, leaking memory, while that is not what you want.



    A better option might be to subscribe whenever it appears and unsubscribe when it disappears as suggested in the comments. Is there any reason not to do that?



    You could also subscribe in the constructor and unsubscribe in the disappearing, but then the subscribe event will never happen again if the page is only instantiated once.



    Long answer short; you will want to unsubscribe. So as long as you can cover that and make it work with your requirements, subscribing in your constructor is fine.






    share|improve this answer

























      up vote
      0
      down vote













      You're already answering your own question basically. You can, of course, subscribe here. But you will need to find a point in your page/app's lifecycle to unsubscribe. Else, this page might be alive forever, leaking memory, while that is not what you want.



      A better option might be to subscribe whenever it appears and unsubscribe when it disappears as suggested in the comments. Is there any reason not to do that?



      You could also subscribe in the constructor and unsubscribe in the disappearing, but then the subscribe event will never happen again if the page is only instantiated once.



      Long answer short; you will want to unsubscribe. So as long as you can cover that and make it work with your requirements, subscribing in your constructor is fine.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You're already answering your own question basically. You can, of course, subscribe here. But you will need to find a point in your page/app's lifecycle to unsubscribe. Else, this page might be alive forever, leaking memory, while that is not what you want.



        A better option might be to subscribe whenever it appears and unsubscribe when it disappears as suggested in the comments. Is there any reason not to do that?



        You could also subscribe in the constructor and unsubscribe in the disappearing, but then the subscribe event will never happen again if the page is only instantiated once.



        Long answer short; you will want to unsubscribe. So as long as you can cover that and make it work with your requirements, subscribing in your constructor is fine.






        share|improve this answer












        You're already answering your own question basically. You can, of course, subscribe here. But you will need to find a point in your page/app's lifecycle to unsubscribe. Else, this page might be alive forever, leaking memory, while that is not what you want.



        A better option might be to subscribe whenever it appears and unsubscribe when it disappears as suggested in the comments. Is there any reason not to do that?



        You could also subscribe in the constructor and unsubscribe in the disappearing, but then the subscribe event will never happen again if the page is only instantiated once.



        Long answer short; you will want to unsubscribe. So as long as you can cover that and make it work with your requirements, subscribing in your constructor is fine.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 7 hours ago









        Gerald Versluis

        15.5k43155




        15.5k43155






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203315%2fis-it-okay-to-have-a-messagecenter-subscribe-in-a-page-constructor%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff