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.
xamarin xamarin.forms
add a comment |
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.
xamarin xamarin.forms
stackoverflow.com/questions/29252351/… Why don't you subscribe inappearing
and unsubscribe indisappearing
?
– Felix D.
8 hours ago
add a comment |
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.
xamarin xamarin.forms
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
xamarin xamarin.forms
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 inappearing
and unsubscribe indisappearing
?
– Felix D.
8 hours ago
add a comment |
stackoverflow.com/questions/29252351/… Why don't you subscribe inappearing
and unsubscribe indisappearing
?
– 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 7 hours ago
Gerald Versluis
15.5k43155
15.5k43155
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
stackoverflow.com/questions/29252351/… Why don't you subscribe in
appearing
and unsubscribe indisappearing
?– Felix D.
8 hours ago