I have a Pivot on MainPage.xaml; how can I call this from another class?
MainPage.mypivot.Items.Add(p);
Error 2 An object reference is required for the non-static field, method,
or property '...MainPage.mypivot' H:\Users\Lacroix\documents\...
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t do that exactly because of the reason the compiler tells you. MainPage is not a static class, when you view the page an instance of the class is being displayed, so you need to manipulate that instance. What you can do is pass a reference to the MainPage class to the other class. Then have MainPage contain an instance of the other class.
In MainPage.xaml.cs
Now,
MyClasswill have access to thePivot(and all other elements of MainPage).If whatever logic the other class uses to add items to the
Pivotis not available when displaying MainPage, i.e. you need to add items when displaying some other page, you’ll need to create a static list (sayPivotItemsList) in theAppclass. Have the other class add items to this list instead of directly to thePivot. You can then access this within the MainPage constructor asApp.PivotItemsListand add the items to thePivot.