Updating Multiple Pages Controls on Windows Phone

Everyone, I'm new to Windows 7 Phone. My situation is that I have a master page containing ScrollViewer

which in turn contains StackPanel

. I want to fill this with a StackPanel

few sub StackPanel

(at runtime) that should hold the Thumb Nail image with a hyperlink and some basic information about the image.

It's okay when I do it from the master page, but I want to know how to update this control (which is on the master page), but from any page other than the master page. I would like to know what is considered the best practice for updating a page control (as described above) from another page.

There are obviously several ways to transfer data between pages

PhoneApplicationService.Current.State["yourparam"] = param
NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative));

      

then on another page just

var k = PhoneApplicationService.Current.State["yourparam"];

      

and many others. But what is the best way to update a shared control from another page?

Note. There are many questions about data access and transfer between pages.

and much more. This is not what I am asking for.

+3


source to share


2 answers


If I understand your question correctly, you are trying to update a control that is included, such as MainPage.xaml from another page, such as the .2.amam page.

As far as I know, there is no way to find the page controls from another page, and this seems unnecessary for the cases I can think of.

The method used to achieve what you are trying to accomplish is usually done by triggering an action (such as clicking a button) and passing a parameter to the page you are trying to refresh. And on that page onnavigatedto event (or viewmodel constructor if you are using MVVM pattern), update your control based on passed parameter.



If your update is data driven, the best practice is to bind an observable collection or object that extends INotifyPropertyChanged (basically any object that can signal one of their properties to the ui) and modify the data based on the parameter passed.

If these two pages are somehow visible at the same time and there is no need for navigation between them (like a ui-type popup or sliding menu), you can make the page you show in a usercontrol popup and bring it to the parent control with that. Parent.

I might be more helpful if you can explain more about your application flow.

+3


source


MVVM pattern would be a good way to go. Talking MVVM is too hard for small teams, not entirely accurate - the goal of MVVM is to separate Silverlight or WPF code. Using code in a Silverlight page to directly access data creates a link in your code and charges technical debt. Regardless of whether you are one developer or 100 if your UI is compatible with your data classes, if you need to change your data classes, you will have to make changes to every UI element that uses those classes. This takes longer and makes it difficult to change the application.

MVVM makes it so your UI (view) doesn't know anything about the data (your model). The ViewModel is the code between the UI that manages the events in the UI that should be persisted in the Model, as well as the changes in the Model that should be presented in the View. For this reason, it handles events and what you think is in your code is an event that can exist outside of your code that can update its associated views when data changes. If you have two pages, an event on one of the pages will be sent to the ViewModel, which will change the model (data) if necessary and bring it back to the ViewModel. The ViewModel will then update any UI element (Views) associated with this chunk of data.



There's a REALLY good demonstration of how to implement the MVVM design pattern here , Guy goes through and takes a typical WPF application (like Silverlight) where the UI codebehind implements event handlers that access the data directly and refactor it using the MVVM pattern.

+2


source







All Articles