jimbrowning
jimbrowning

Reputation: 39

How to pass objects through multiple pages in UWP

I am tasked with creating a multi-page UWP app that needs to pass a student object through multiple pages. The app starts on the main page where there are two buttons, one to add a new student and then and the other to then view the students details.

I am currently struggling trying to figure out how to pass the student object from the "new student page" to the "student details" page. I'm not allowed to store the students information in a file or database.

When a new student has been added the information should be stored within the Student object, is it possible to make this object public so that it can be used without passing it through pages? Is it also possible to bind the text blocks on the student details page to the student object that is from the input page?

Upvotes: 2

Views: 2208

Answers (2)

Luca Lindholm
Luca Lindholm

Reputation: 821

This isn't good way of resolving this problem.

You see… an expert developer sees the UI only as the "presentation layer" for app data, NOT as the main logic layer.

The correct way to do it is creating a static entity that acts as the "engine" of the logic of your app and that is present during all the app's session.

The usual way to implement this, in fact, is by using the standard MainPage as the "shell" of the app, containing a static field (named "Current") that redirects to the MainPage itself, and an AppViewModel class, that contains all the app data and logic. Then you access the MainPage.Current.ViewModel data by binding all the XAML controls to it.

Best regards

Upvotes: 2

MKH
MKH

Reputation: 367

You can use your Student class object and then pass it to another page when Navigate to it as parameter.

In New Student page :

void ButtonShowDetails_Click(sender , ....)
{
  var student = new Student();
  student.Name="Joe";// or txtStudentName.Text
  /// etc
  /// etc
  Frame.Navigate(typeof(pgStudentDetails), student);
}

In pgStudentDetails page :

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    var param = (Student)e.Parameter; // get parameter
    txtName.Text= param.Name;//show data to user
}

Upvotes: 5

Related Questions