c# - Silverlight:在页面之间传递复杂对象

标签 c# .net silverlight .net-3.5

在 pageA 中我有一个链接到 pageB 的 HyperlinkBut​​ton

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

如何使 pageA 上的 COMPLEX 对象对 pageB 可用?

要么在我重新创建 pageB 时将其传入,要么将其设为 pageA 上我可以访问的公共(public)属性?

我可以将该对象添加到 App.xaml 以便它随处可用,但我认为这不是最佳做法

最佳答案

我认为最简单的方法是使用全局 Context 实现来设置/获取数据。

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

如果你有多个变量,你可以使用字典根据键设置/获取值

关于c# - Silverlight:在页面之间传递复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2047054/

相关文章:

wcf - 没有从客户端计算机上的 Silverlight 发送 WCF 请求

c# - 是否有适用于 Silverlight 的 C# 动态模拟框架?

c# - 包含 Entity Framework 的通用 Find()

c# - 从模型中删除数据注释并将它们放在单独的类中

.net - 如何完全禁用log4net

c# - Backgroundworker C#中 "WorkerSupportCancellation"的意义是什么

c# - 通过工作线程的异步接口(interface)使用 WCF 服务,如何确保从客户端发送事件 "in order"

c# - 抽象类的子类,总是调用抽象构造函数

c# - 使用继承向内置类添加函数

c# - 将字节作为参数传递给 c#?