Helminth
Helminth

Reputation: 60

How to pass value from one Silverlight page to another?

I have two .xaml pages LoginPage and child page - Workloads_New . I need to pass LoginID from LoginPage to Workloads_New. But in Workloads_New I keep getting LoginID value 0. Here is my code in LoginPage:

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

and in Workloads_New I have:

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

Upvotes: 0

Views: 1830

Answers (2)

Mohammad Atiour Islam
Mohammad Atiour Islam

Reputation: 5708

The UriMapper object also supports URIs that take query-string arguments. For example, consider the following mapping:

In XAML :

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

In C# you can also see this

consider the following code, which embeds two numbers into a URI as query-string arguments:

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

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

A typical completed URI might look something like this:

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

You can retrieve the product ID information in the destination page with code like this:

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"]);

Upvotes: 4

Helminth
Helminth

Reputation: 60

I found answer.

In App.xaml.cs

public int LoginID { get; set; }

In LoginPage.xaml.cs where I set LoginID value I wrote

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

In Workloads_New.xaml.cs where I use LoginID I wrote

this.ErrorBlock.Text = ((App)App.Current).LoginID.ToString();

Upvotes: 1

Related Questions