在 pageA 中我有一个链接到 pageB 的 HyperlinkButton
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/