• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Programming Help C#/silverlight

phantom404

Golden Member
I'm pretty new to C# and silverlight I'm trying to pass variable values between 2 pages. Here is what I'm using.

Uri go = new Uri("/CharStats.xaml?var1=" + var1.Content + "var2=" + var2.txt, UriKind.Relative);

I was wondering how to parse the string on the page I'm passing to. I know how to do it if its a single variable but not more than one:

string stringname;
NavigationContext.QueryString.TryGetValue("search", out stringout);

Is there a better way to pass variables between pages? This is for a WP7 application btw. Thanks.
 
I am assuming you're using ASP.NET. Take a look at this example: http://dotnetperls.com/querystring. It talks about passing multiple query strings using the URI. In ASP.NET, you can also utilize SessionState, but frankly that would be a stretch if you're simply trying to pass around strings.
 
You forgot an ampersand in your query string.

Uri go = new Uri("/CharStats.xaml?var1=" + var1.Content + "var2=" + var2.txt, UriKind.Relative);

should be

Uri go = new Uri("/CharStats.xaml?var1=" + var1.Content + "&var2=" + var2.txt, UriKind.Relative);

As for a better way to pass data around, it really depends on what you're trying to do. That's the accepted method for people who are mostly doing web development, but if you're building something substantial you may want to look into one of the MVVM frameworks, although this might be a bit much for someone who is just learning to program.

Also some people might tell you that using NavigationContext.QueryString.TryGetValue() is an antipattern. I'm not convinced myself, but out parameters are considered a no-no by many programmers. I myself prefer to use
NavigationContext.QueryString["key"]
although be aware it will through an exception if you use an invalid key.
 
Back
Top