This project is read-only.

Mapping view to VM without navigation

Nov 15, 2010 at 10:22 PM

Is it possible to map a view to a VM without navigating to the view? I am adding the view as UserControl to a page. I would like the view and VM to be mapped using the nRoute mechanisms if possible.

Thanks in advance for any help.

 

Nov 15, 2010 at 11:04 PM

Absolutely, in fact navigation and View-ViewModel pairing is independent of each other - which is to say, you can create View-ViewModel pairing (use either MapView, MapViewModel or DefineViewViewModel attributes) without using navigation and you can earmark a visual as being navigable (using MapNavigationContent or MapNavigationResource attributes) without having to use backing ViewModel.

Rishi

Nov 16, 2010 at 8:56 AM

I had tried that but it wasn't working for some reason. However I think I might use another approach to solve this issue. Can you let me know what you think please?

I am creating a dashboard that can display any number of widgets. When the dashboard is loaded the layout it deserialized and for each widget placeholder a LoadWidget event is triggered passing the widgets ID. What I was planning to do is retrieve the widget using the ID and then set the widget as the content for each widget placeholder. But now I am thinking that a better approach might be to add a statefulContainer to each widget placeholder within the dashboard and then set the URL to be the widget view URL and the widget ID as a parameter. This approach will instantiate a new view and VM passing in the widget ID for each Widget within the dashboard.

 

Nov 16, 2010 at 11:58 AM

Huh, not working.. if you can use it with navigation then you should be able to use it without - since they are not directly connected. For example map your ViewModel:

[MapViewModel(typeof(SampleView))]
public class SampleViewModel 
{
    // your ViewModel stuff goes here
}

and then in your SampleView control:

<UserControl ... >
    <i:Interaction.Behaviors>
        <n:BridgeViewModelBehavior />
    </i:Interaction.Behaviors>

    <!-- Your View goes here -->
</UserControl>

and that's it - it should all work. 

Well, as for your dashboard question - I've done something similar to that, and it works quite well. However, if you don't need to use state-management functionality you can use a light weight navigation container (like NavigationContentControl) or even just write a simple navigation adapter for use with a placeholder.

Hope this helps,
Rishi

Nov 17, 2010 at 8:53 AM

Thanks for the help. The problem I was having wth mapping to the VM was caused because a service that is Initialised "whenAvailable" was trying to use the view and I am guessing that the VM hadn't been "discovered" yet by nRoute. I changed the service to be on demand and eveything works ok now.