Svandal
Svandal

Reputation: 1

How to manipulate one object on two different xaml pages (Send object from one xaml page to another)

Can anybody help me with this universal app I am making for windows 8.1 in xaml and C#? I have tried to search for answers to this question, but all I could find was that there are no good ways of doing this. If there are no good ways of passing objects from one page to another I would like to hear what other options I have.

In my main page I have made two lists of objects, which I made from reading a file. I want to use these objects in most of my other pages, but I can not find out how to manipulate these lists of objects without making them global (which I have heard is not an ideal thing).

Specific what I want to accomplish: I am making a D&D app, and have created a list of the object "Monster" which contains stuff like name, hit points, attack, etc. And I have created a list of object called "Weapons" which contains stuff like name, damage, etc. Now I want to show the name and hit points of the monster on my main page, and when I click on this monster I want to create a new page where everything about this object (monster) is shown. And if I change the "hit points" value (if this monster takes damage) on one page, the other page should also be updated with the same information.

Now I want to just pass along these lists to the next page, which should solve my problem, but I have not managed to find a way to do this. I have only managed to pass along a simple string from one page to another.

//This is from the main menu where I have created my lists of objects, when I push the button I should go to the next page.

private void ButtonShowMonster_Click(object sender, RoutedEventArgs e)
        {            
if (this.Frame != null)
            {

this.Frame.Navigate(typeof(OneMonsterPage));
            }
        }

Edit1: Is the best way of doing this simply by creating a global list objects?

Upvotes: 0

Views: 220

Answers (1)

Peter Torr
Peter Torr

Reputation: 12019

The easiest way to do this is to have a "global-ish" list of objects, eg a property of the App object, or maybe a static class or singleton object that held the values. This is commonly called the "model" for your app. Then you can access the data from different pages, and you can (eg) simply pass the index of the selected object between pages with the Navigate overload that takes an extra parameter.

In general, you should do some research on the MVVM pattern for app design.

Upvotes: 2

Related Questions