Programming Help C#/silverlight

phantom404

Golden Member
Nov 2, 2004
1,460
2
81
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.
 

Oyster

Member
Nov 20, 2008
151
0
0
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.
 
Oct 27, 2007
17,009
5
0
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.