简体   繁体   中英

update control on another page windows 8 metro app c#

This is a basic process that I cannot find any info on in Google or reference books on my Kindle account.

In regular forms applications pre-Windows 8, if you are on form 2 and you want to update something on form 1, you just type:

form1.Default.controlOnForm1.text = "updated text";

This does not work in Windows 8.

Thank you.

UPDATE: I found an easy way to do this in cases when databinding does not do what I need. I simply create a static copy of the control I need to access. Say Button1 on page2 needs to be accessed on page1.

At the top declare:

public static Button statButton1;

in the loaded event I create the relationship between the static copy and the button in the XAML code:

statButton1 = Button1;

And then easy as pie, you can access the button anywhere:

page2.statButton1.Width = 48;

Windows 8 apps (or any other XAML based apps) are usually made using the MVVM Pattern. I really recommend you read about it and do the same. This pattern leads to less coupling in the application and makes it easier to develop and maintain.

The type of changes you are talking about here would be done by setting a property on the ViewModel (VM), that in turn, notifies the View (UI) with a PropertyChangedEvent so it can refresh itself.

To allow the ViewModels to set each others properties, they would all need to know about each other, which in turn leads to high coupling. This is usually solved by using an EventAggregator or MessageBus which sends messages/events between objects (without them knowing about each other) based on a subscribe/publish pattern.

To start out I would look at An Address Book Application Made in MVVM for Metro . This is a basic example which shows the usage of this pattern without any frameworks.

Once you feel comfortable with the MVVM pattern, I would suggest you use a framework like Caliburn.Micro or MVVM Light . These frameworks offer lots of great stuff for building applications with the MVVM pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM