2010-11-15 183 views
0

我有两个.xaml页面LoginPage和子页面 - Workloads_New。我需要将LoginID从LoginPage传递到Workloads_New。但在Workloads_New我不断收到登录ID值0.这是我在LoginPage代码:如何将值从一个Silverlight页面传递给另一个?

void webService_GetUserIDCompleted(object sender, GetUserIDCompletedEventArgs e) 
{ 
int ID = e.Result; //for example i get ID=2 
if (ID > 0) 
    { 
    this.Content = new MainPage(); 
    Workloads_New Child = new Workloads_New(); 
    Child.LoginID = ID; //In debug mode i see that ID=2 and LoginID=2 
    } 
} 

和Workloads_New我:

public int LoginID { get; set; } 

private void ChildWindow_Loaded(object sender, RoutedEventArgs e) 
{ 
    //to test i just want to see that id in textblock but i keep getting LoginID=0 why? 
    this.ErrorBlock.Text = this.LoginID.ToString(); 
} 
+1

你在哪里安装了Workloads_New到UI ?如果您没有将它附加到UI,我认为您创建的Workloads_New上的childWindow_Loaded不会被调用。 – Stephan 2010-11-15 21:53:28

+0

不,它调用并写入ErrorBlock.Text - “0” – Helminth 2010-11-18 15:05:38

回答

4

UriMapper对象还支持带有查询字符串参数的URI。例如,考虑 以下映射:

在XAML:

<navigation:UriMapping Uri="Products/{id}" 
MappedUri="/Views/ProductPage.xaml?id={id}"></navigation:UriMapping> 

在C#中,你也可以看到这个

考虑下面的代码,嵌入两个数字为URI as 查询字符串参数:

string uriText = String.Format("/Product.xaml?productID={0}&type={1}",productID, productType); 

mainFrame.Navigate(new Uri(uriText), UriKind.Relative); 

典型完成URI可能是这个样子:

/Product.xaml?productID=402&type=12 

您可以检索到目标页面代码的产品ID信息是这样的:

int productID, type; 
if (this.NavigationContext.QueryString.ContainsKey("productID")) 
productID = Int32.Parse(this.NavigationContext.QueryString["productID"]); 
if (this.NavigationContext.QueryString.ContainsKey("type")) 
type = Int32.Parse(this.NavigationContext.QueryString["type"]); 
+0

这是一个很好的答案;请发布更多此类信息,而不是仅链接到您的网站。 – 2012-12-10 04:12:14

1

我找到了答案。

在App.xaml.cs

public int LoginID { get; set; } 

在LoginPage.xaml.cs在哪里设置登录ID值,我写

((App)App.Current).LoginID = ID; 

在Workloads_New.xaml.cs,我使用登录ID,我写

this.ErrorBlock.Text = ((App)App.Current).LoginID.ToString(); 
相关问题