c# - How to make a global property that is visible within all pages in a Windows 10 app? -
i thinking of app represents store, using gridview view items , data represented observablecollection.
my xaml code :
xmlns:data="using:itemsstore.models" <gridview itemssource="{x:bind itemslist}"> <gridview.itemtemplate> <datatemplate x:datatype="data:item"> <stackpanel orientation="vertical"> <image source="{x:bind imagesource}"/> <textblock text="{x:bind name}"/> <textblock text="{x:bind disc}"/> </stackpanel> </datatemplate> </gridview.itemtemplate> </gridview>
the c# code in mainpage :
public sealed partial class mainpage : page { public observablecollection<item> itemslist; public mainpage() { this.initializecomponent(); itemslist = new observablecollection<item>(); } }
i added button , input controls add new items itemslist , works fine, want make page contains controls , logic add new items list, made addnew.xaml page couldn't access observablecollection in mainpage add new items it, , tried make observablecollection static field , managed access collection in mainpage saw no change in mainpage after updating collection in addnew page.
i think problem because of initializing statement in mainpage constructor , every time navigate addnew page , update collection , navigate mainpage constrctor gets called , collection reset, solution making observablecollection golbal variable , initializing somewhere out of mainpage constructor , or initialze collection within event handler excutes once when app launces.
so questions : 1- there way make global observablecollection visible every page in app ? if so, how can refrence in binding statement (x:bind theglobalcollection) or
2- there event trigged once through app life time ?
i sorry big question, time.
if understood correct, remove instatiation constructor , change field this:
public static observablecollection<item> itemslist = new observablecollection<item>();
in way, itemslist instantiated once.
Comments
Post a Comment