Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, no, not that's built-in. Complex parameters like IRepository instances are, unfortunately, beyond the ability of the navigation facilities in Silverlight; I usually use some form of IoC container to handle those. Simpler POCO parameters are easily serialized to a string, but that still requires magic strings and manual query-string parsing.</p> <p>You can, however, easily build something typesafe yourself. For example, here's my approach.</p> <p>For parameter data, I have a class that I call 'Extras', which wraps a <code>Dictionary&lt;string, object&gt;</code> with methods like <code>GetBool(string)</code>, <code>GetInt32(string)</code>, etc., and has a static factory method <code>CreateFromUri(Uri)</code>; this is good enough for my purposes.</p> <p>I use this in conjunction with type-safe navigation. I really like the MVVM pattern, and each of my pages has a ViewModel encapsulating nearly all logic. The one-to-one relationship of page to ViewModel makes the latter an ideal navigation key. That, combined with attributes and reflection, gives us a simple solution:</p> <pre><code>public class NavigationTargetAttribute : Attribute { private readonly Type target; public ViewModelBase Target { get { return target; } } public NavigationTargetAttribute(Type target) { this.target = target; } } </code></pre> <p>Put one of these on each of your pages, with the proper ViewModel type.</p> <pre><code>[NavigationTarget(typeof(LoginViewModel))] public class LoginPage : PhoneApplicationPage { ... } </code></pre> <p>Then, in a singleton NavigationManager-esque class, you can do:</p> <pre><code>GetType().Assembly .GetTypes() .Select(t =&gt; new { Type = t, Attr = t.GetCustomAttributes(false).FirstOrDefault(attr =&gt; attr is NavigationTargetAttribute) }) .Where(t =&gt; t.Attr != null); </code></pre> <p>And just like that, you have a collection of every navigable type in your app. From there, it's not much more work to put them in a dictionary, for example. If you follow a convention for where you put your pages, you can (for example) translate between type and Uri quite easily... for example, <code>new Uri("/Pages/" + myPageType.Name + ".xaml", UriKind.Relative)</code>. It's not much more to add support for query parameters. Finally, you'll end up with a method, like so:</p> <pre><code>public void Navigate(Type target, Extras extras) { Type pageType; if (navigationTargets.TryGetValue(target, out pageType)) { var uri = CreateUri(pageType, extras); navigationService.NavigateTo(uri); } // error handling here } </code></pre> <p>Finally, in the page's <code>OnNavigatedTo</code> method, I do something like:</p> <pre><code>var extras = Extras.CreateFromUri(e.Uri); ((ViewModelBase) DataContext).OnNavigatedTo(extras); </code></pre> <p>This, finally, gives a semblance of strongly-typed navigation. This is a bare-bones approach; off the top of my head, this could be improved by adding required parameters in the navigation attribute and validating them at navigation-time. It also doesn't support more complex types of navigation, where the value of nav arguments would determine the ultimate destination. Nevertheless, this suits my 90% use case - maybe it will work for you, too.</p> <p>There are definitely some details omitted here, like how exactly to get an instance of <code>NavigationService</code> - I can work up a more complete sample later tonight, but this should be enough to get started.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload