Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

New Post: Methods for providing view model dependencies

$
0
0
The only way I have found to do what you're wanting to do is:

1) Include resolution of the container in the view's constructor and store the container internally
2) Resolve the view using Prism Region
3) In your view's Loaded event handler, use the container to resolve the view model and set that as your data context.

The drawback to this method are as follows:
1) You have to store the container reference internally and use it (not too bad)
2) You have to define an interface specific to the view model so it can be resolved
3) You don't get the benefit of design-time data context awareness when defining bindings to the data context, since the loaded event won't be called.

i.e.:
public interface IShellViewModel
{
    // No requirements
}

public partial class Shell : Window
{
    private IUnityContainer _container { get; set; }

    public Shell(IUnityContainer container)
    {
        _container = container;
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = _container.Resolve<IShellViewModel>();
    }
}

Viewing all articles
Browse latest Browse all 1760

Trending Articles