How to load data in TabbedPage when a tab is clicked?
I am using TabbedPage
for navigation with tabs. All my Page
classes have just an empty default constructor and I load my data in the OnAppearing
method. I have 5 tabs. As soon as I click on the second tab, the OnAppearing
methods of the 3rd, 4th and 5th pages are also called.
How do I ensure that the data is only loaded when I click on the tab?
forms xamarin tabbedpage
add a comment |
I am using TabbedPage
for navigation with tabs. All my Page
classes have just an empty default constructor and I load my data in the OnAppearing
method. I have 5 tabs. As soon as I click on the second tab, the OnAppearing
methods of the 3rd, 4th and 5th pages are also called.
How do I ensure that the data is only loaded when I click on the tab?
forms xamarin tabbedpage
You have to react to OnCurrentPageChanged of the tabbed bar, ignoring it on startup when just adding new tabs. After that you can, for example, send messages to pages upon this event or call their code from your subclassed tabbed page.
– Nick Kovalsky
Dec 29 '18 at 15:05
Hey,did you solve the issue?
– Lucas Zhang - MSFT
Jan 2 at 1:02
add a comment |
I am using TabbedPage
for navigation with tabs. All my Page
classes have just an empty default constructor and I load my data in the OnAppearing
method. I have 5 tabs. As soon as I click on the second tab, the OnAppearing
methods of the 3rd, 4th and 5th pages are also called.
How do I ensure that the data is only loaded when I click on the tab?
forms xamarin tabbedpage
I am using TabbedPage
for navigation with tabs. All my Page
classes have just an empty default constructor and I load my data in the OnAppearing
method. I have 5 tabs. As soon as I click on the second tab, the OnAppearing
methods of the 3rd, 4th and 5th pages are also called.
How do I ensure that the data is only loaded when I click on the tab?
forms xamarin tabbedpage
forms xamarin tabbedpage
edited Dec 31 '18 at 1:34
bunbun
2,02532446
2,02532446
asked Dec 29 '18 at 10:53
M_BM_B
81
81
You have to react to OnCurrentPageChanged of the tabbed bar, ignoring it on startup when just adding new tabs. After that you can, for example, send messages to pages upon this event or call their code from your subclassed tabbed page.
– Nick Kovalsky
Dec 29 '18 at 15:05
Hey,did you solve the issue?
– Lucas Zhang - MSFT
Jan 2 at 1:02
add a comment |
You have to react to OnCurrentPageChanged of the tabbed bar, ignoring it on startup when just adding new tabs. After that you can, for example, send messages to pages upon this event or call their code from your subclassed tabbed page.
– Nick Kovalsky
Dec 29 '18 at 15:05
Hey,did you solve the issue?
– Lucas Zhang - MSFT
Jan 2 at 1:02
You have to react to OnCurrentPageChanged of the tabbed bar, ignoring it on startup when just adding new tabs. After that you can, for example, send messages to pages upon this event or call their code from your subclassed tabbed page.
– Nick Kovalsky
Dec 29 '18 at 15:05
You have to react to OnCurrentPageChanged of the tabbed bar, ignoring it on startup when just adding new tabs. After that you can, for example, send messages to pages upon this event or call their code from your subclassed tabbed page.
– Nick Kovalsky
Dec 29 '18 at 15:05
Hey,did you solve the issue?
– Lucas Zhang - MSFT
Jan 2 at 1:02
Hey,did you solve the issue?
– Lucas Zhang - MSFT
Jan 2 at 1:02
add a comment |
1 Answer
1
active
oldest
votes
Solution:
You can get the index of currentPage in method OnCurrentPageChanged
And if the index equals 1(second page) , use the messagecenter
to send message to the page.Refer the following code .
in Tabbed Page
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
int index = Children.IndexOf(CurrentPage);
if (index == 1)
{
MessagingCenter.Send<Object>(this, "click_second_tab");
}
else if (index == 2)
{
MessagingCenter.Send<Object>(this, "click_third_tab");
}
}
in the second page .Move the code that load data from onAppearing to the constructor
public MyPage1()
{
//...
MessagingCenter.Subscribe<Object>(this, "click_second_tab", (obj) =>
{
//load your data here
Console.WriteLine("11111");
});
}
It worked, thank you very much!
– M_B
Jan 4 at 21:44
add a comment |
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968876%2fhow-to-load-data-in-tabbedpage-when-a-tab-is-clicked%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Solution:
You can get the index of currentPage in method OnCurrentPageChanged
And if the index equals 1(second page) , use the messagecenter
to send message to the page.Refer the following code .
in Tabbed Page
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
int index = Children.IndexOf(CurrentPage);
if (index == 1)
{
MessagingCenter.Send<Object>(this, "click_second_tab");
}
else if (index == 2)
{
MessagingCenter.Send<Object>(this, "click_third_tab");
}
}
in the second page .Move the code that load data from onAppearing to the constructor
public MyPage1()
{
//...
MessagingCenter.Subscribe<Object>(this, "click_second_tab", (obj) =>
{
//load your data here
Console.WriteLine("11111");
});
}
It worked, thank you very much!
– M_B
Jan 4 at 21:44
add a comment |
Solution:
You can get the index of currentPage in method OnCurrentPageChanged
And if the index equals 1(second page) , use the messagecenter
to send message to the page.Refer the following code .
in Tabbed Page
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
int index = Children.IndexOf(CurrentPage);
if (index == 1)
{
MessagingCenter.Send<Object>(this, "click_second_tab");
}
else if (index == 2)
{
MessagingCenter.Send<Object>(this, "click_third_tab");
}
}
in the second page .Move the code that load data from onAppearing to the constructor
public MyPage1()
{
//...
MessagingCenter.Subscribe<Object>(this, "click_second_tab", (obj) =>
{
//load your data here
Console.WriteLine("11111");
});
}
It worked, thank you very much!
– M_B
Jan 4 at 21:44
add a comment |
Solution:
You can get the index of currentPage in method OnCurrentPageChanged
And if the index equals 1(second page) , use the messagecenter
to send message to the page.Refer the following code .
in Tabbed Page
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
int index = Children.IndexOf(CurrentPage);
if (index == 1)
{
MessagingCenter.Send<Object>(this, "click_second_tab");
}
else if (index == 2)
{
MessagingCenter.Send<Object>(this, "click_third_tab");
}
}
in the second page .Move the code that load data from onAppearing to the constructor
public MyPage1()
{
//...
MessagingCenter.Subscribe<Object>(this, "click_second_tab", (obj) =>
{
//load your data here
Console.WriteLine("11111");
});
}
Solution:
You can get the index of currentPage in method OnCurrentPageChanged
And if the index equals 1(second page) , use the messagecenter
to send message to the page.Refer the following code .
in Tabbed Page
protected override void OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
int index = Children.IndexOf(CurrentPage);
if (index == 1)
{
MessagingCenter.Send<Object>(this, "click_second_tab");
}
else if (index == 2)
{
MessagingCenter.Send<Object>(this, "click_third_tab");
}
}
in the second page .Move the code that load data from onAppearing to the constructor
public MyPage1()
{
//...
MessagingCenter.Subscribe<Object>(this, "click_second_tab", (obj) =>
{
//load your data here
Console.WriteLine("11111");
});
}
answered Jan 1 at 7:56
Lucas Zhang - MSFTLucas Zhang - MSFT
1,916228
1,916228
It worked, thank you very much!
– M_B
Jan 4 at 21:44
add a comment |
It worked, thank you very much!
– M_B
Jan 4 at 21:44
It worked, thank you very much!
– M_B
Jan 4 at 21:44
It worked, thank you very much!
– M_B
Jan 4 at 21:44
add a comment |
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.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968876%2fhow-to-load-data-in-tabbedpage-when-a-tab-is-clicked%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
You have to react to OnCurrentPageChanged of the tabbed bar, ignoring it on startup when just adding new tabs. After that you can, for example, send messages to pages upon this event or call their code from your subclassed tabbed page.
– Nick Kovalsky
Dec 29 '18 at 15:05
Hey,did you solve the issue?
– Lucas Zhang - MSFT
Jan 2 at 1:02