开发者

Silverlight: Passing a complex object between pages

In pageA I have a HyperlinkButton that links to pageB

    private void Link1_Click_1(obje开发者_如何学Pythonct 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


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


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...


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;


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


In terms of silverlight everything is run on the client side so its different then client server architecture like we have in asp.net. You could create a static class, have some public property in it, you could access it to any other page.

public class Cache
{
   private static Cache _cache;

   private Cache()
   {}

   public Cache Instance
   {
       get
       {
          if(_cache == null)
               _cache = new Cache();
          return _cache;
       }
   } 
   public object CachedData
   {get; set;}
}

In page one, you could do Cache.Instance.CachedData = (object)"Hello World"; In page two, you could do string Data = (string)Cache.Instance.CachedData;

Please mark it if you find it helpful.


Depends on the scenario, but isolated storage may be an option worth considering. It is intended to be used for storing data client-side to reduce load on the server and this would seem to be an ideal scenario based on what you have described.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜