Silverlight: passing a complex object between pages

In pageA I have a HyperlinkButton link 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 accessible to pageB?

Either pass it when I recreate pageB or make it a public property on page A, which I can access, I think?

I could add an object in App.xaml so that it is accessible everywhere, but I don't think the best rating

+3
source share
6 answers

I think the easiest way is to use a global context implementation to set / receive 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

, /

+2

, , () . , , , , , , . , , Initialize (StatusObject status), , . PageB. - Navigated , , NavigationEventArgs.Content. , , , ...

0

Session .

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;
0

, , - DataContext PageA, pageB

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

silverlight , , , asp.net. , - , .

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;}
}

 Cache.Instance.CachedData = () "Hello World"; string Data = () Cache.Instance.CachedData;

, , .

0

Script dependent, but isolated storage can be considered an option. It is designed to store data on the client side to reduce server load, and this is apparently the perfect scenario based on what you described.

0
source

Source: https://habr.com/ru/post/1728139/


All Articles