views:

277

answers:

4

Hi there,

In pageA I have a HyperlinkButton that links to pageB

    private void Link1_Click_1(object sender, RoutedEventArgs e)
    {
        HyperlinkButton btn = sender as HyperlinkButton;
        string url = btn.Tag.ToString();

        this.mainFrame.Navigate(new Uri(url, UriKind.Relative));  
    }

How can I make a COMPLEX object on pageA available to pageB?

Either passing it in when I recreate pageB or making it a public property on pageA that I can access I guess?

I could add the object to App.xaml so that it's available everywhere but I don't think that's best pratice

A: 

I think the simplest way is to use a global Context implementation to set/get your data.

public class Context
{

   static Context _context = null;     
   static object sync = new object();        
   public object Data { get; set; }

   private Context()
   {
   }

   public static Context GetContext()
   {
      if _context == null) 
      {
         lock (sync)
         {
            if _context == null)
            {
               _context = new Context(); 
            }
         }
       }
       return _context;
   }
}

//Load your data, and on any page you need it, just do:
Context c = Context.GetContext();

//set or get c.Data here

If you have multiple variables, you may use a Dictionary to set/get values based on keys

amazedsaint
That will work but I would rather not use static variables. There must be other ways?
Billy
@Billy, using static variables isnt bad, one set of Global variables are always used in almost every systems, otherwise you end up doing too much of passing (Marshalling) code. However there is always trade off between when to use and when not to use. But in your case, when object needs to be accessed via multiple classes, its good to put it in Global static variable.
Akash Kava
Agreed with Akash. How ever, if you've problem with Static variables, you can expose a Dictionary<string,object> type property in your App.xaml.cs, and then get/set values from any page, like App ap = (App)Application.Current; ap.YourProperty=value;
amazedsaint
A: 

It's always a good idea to minimize the amount of data you put in global or static structures, but sometimes you can't (easily) get around it. Still, you want to avoid it if you can, as having too many static values floating around can lead to very odd side-effects that are difficult to debug. What I've generally done is to include an Initialize(StatusObject status) method on my pages that I call on the instance that gets created when the hyperlink is clicked. The hard part is actually getting a handle to the new instance of PageB. The best way to do that is to hook the Navigated event on the frame that will be navigating, and grab the NavigationEventArgs.Content value. Of course, this means you have to worry about timing, since navigation happens asynchronously...

Ken Smith
A: 

You can also use Session to transfer your object from one page to another.

E.g.:-

Page A:
MyComplexObject complex = new MyComplexObject();
Session["cObj"] = complex;

Page B:
if(Session["cObj"] != null){
MyComplexObject new_complex = (MyComplexObject)Session["cObj"];
}

or
MyComplexObject new_complex = Session["cObj"] as MyComplexObject;
Bhaskar
A: 

Best way I could find was to set the DataContext of the frame on PageA and then in pageB I can access the data with:

((System.Windows.Controls.Frame)this.Parent).DataContext
Billy