Im trying to use MVVM on a PRISM Module. I have a ViewModel in my module with a parameterized constructor which accepts an IOutputService object which will be injected using Ninject.
namespace HelloWorld.ViewModels
{
public class HelloWorldViewModel : ViewModelBase
{
private IOutputService outputService;
public HelloWorldViewModel(IOutputService outputService)
{
this.outputService = outputService;
}
}
}
In the HelloWorldModule.cs file, I register IOutputService with a class that implements it.
public class HelloWorldModule : IModule
{
private IKernel kernel;
private IRegionManager regionManager;
public HelloWorldModule(IKernel kernel, IRegionManager regionManager)
{
this.kernel = kernel;
this.regionManager = regionManager;
}
public void Initialize()
{
kernel.Bind<IOutputService>().To<MessageBoxOutputService>();
regionManager.RegisterViewWithRegion("Region1", typeof(HelloWorldView));
}
}
You can also notice that I am registering the HelloWorldView to Region1. HelloWorldView uses HelloWorldViewModel. The problem now is I can't initialize the HelloWorldViewModel in XAML of View because my ViewModel doesn't have a parameterless constructor.
<UserControl x:Class="HelloWorld.Views.HelloWorldView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:HelloWorld.ViewModels"
mc:Ignorable="d">
<UserControl.DataContext>
<vm:HelloWorldViewModel />
</UserControl.DataContext>
<Grid>
</Grid>
</UserControl>
When I run this, the InitializeComponent() method of the View throws an NullReferenceException. Any proper way to make this work? Thanks.
In the codebehind of the view, inject the viewmodel in the constructor.
public partial class HelloWorldView : UserControl
{
public HelloWorldView(HelloWorldViewModel vm)
{
InitializeComponent();
DataContext = vm;
}
}
Well You don't have to set View's DataContext in xaml. It can be simply done in CodeBehind. Add following property to HelloWorldView.xaml.cs
/// <summary>
/// Gets or sets ViewModel.
/// </summary>
[Inject]
public HelloWorldViewModel ViewModel
{
get { return this.DataContext as HelloWorldViewModel; }
set { this.DataContext = value; }
}
When you call RegisterViewWithRegion an instance of that view will be resolved and thanks to InjectAttribute Ninject will provide that View with DataContext. So you don't need to worry about that. Constructor injection should work as well.
Try using ObjectDataProvider.
Example (From MSDN):
<ObjectDataProvider x:Key="myDataSource" ObjectType="{x:Type src:Person}">
<ObjectDataProvider.ConstructorParameters>
<system:String>Joe</system:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
Here is a good example by Bea Stollnitz:
Why should I use ObjectDataProvider?
Related
I'm trying to get my head around WPF, Unity and MvvMlight (galasoft). So far my little set up works. If I run my application the label is filled with a random name generated by my DataService. (small victory getting all moving parts to work)
But in the design view of Visual Studio the label remains empty. How do i convince VisualStudio to render some 'design time' data in my label?
I'm using: Visual Studio Premium 2013, Unity 4.0.1, MvvmLight 5.2, .net 4.5
App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
IUnityContainer container = new UnityContainer();
container.RegisterType<IDataService, DataService>();
container.RegisterType<IMainViewModel, MainViewModel>();
MainWindow mainWindow = container.Resolve<MainWindow>();
mainWindow.Show();
base.OnStartup(e);
}
}
In App.xaml I have not defined the StartUpUri
MainWindow.xaml
<Window x:Class="UnityMvvmTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="500">
<Grid>
<Label x:Name="myLabel" Content="{Binding MyText}"/>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public MainWindow(IMainViewModel theViewModel)
: this()
{
this.DataContext = theViewModel;
}
}
MainViewModel.cs
public class MainViewModel : ViewModelBase, IMainViewModel
{
private readonly IDataService _dataService;
public MainViewModel(IDataService dataService)
{
_dataService = dataService;
if (IsInDesignMode)
{
// Code runs in design time data.
MyText = "Design Data";
}
else
{
// Code runs "for real"
MyText = _dataService.GetName();
}
}
public string MyText { get; set; }
}
I found a method, using hints from https://stackoverflow.com/a/3380895/249845
I created a second (flat) implementation of IMainVieModel in a separate namespace: UnityMvvmTest.ViewModel.Design. This implementation has no logic, it just fills the properties so the designer has some data to display.
This implementation is used in design time, since it is specified as the DesignTime DataContext. (with xmlns:d, xmlns:mc and xmlns:vm). The mc-namespace is needed to hide the d-namespace during runtime, see why.
The result is 5 extra lines in the Xaml, an extra (almost empty) implementation of IMainViewModel. And an extra (empty) constructor in code behind, instead of a constuctor that test for IsInDesignMode. This isn't a big deal, since unity will pick the constructor with the most parameters it can resolve.
MainWindow.xaml
<Window x:Class="UnityMvvmTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:UnityMvvmTest.ViewModel.Design"
d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=vm:MainViewModel}"
mc:Ignorable="d"
>
<Grid>
<Label x:Name="myLabel" Content="{Binding MyText}" />
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
// Contructor used in DesignTime
public MainWindow()
{
InitializeComponent();
}
//Constructor used by Unity
public MainWindow(IMainViewModel theViewModel)
: this()
{
this.DataContext = theViewModel;
}
}
MainViewModel.cs (design time implementation)
namespace UnityMvvmTest.ViewModel.Design
{
public class MainViewModel : IMainViewModel
{
public MainViewModel()
{
MyText = "my Design time data";
}
public string MyText { get; set; }
}
}
I am trying to understand how to switch views and its viewmodels in a wpf mvvm application which uses prism and unity. I put something together from a tutorial but have additional question bec a few things dont seem right. What I have so far is a WPF application with a shell.xaml window that has section placeholders using prism regions. In addition, I have a bootstrapper class that register modules which will fill in the different regions in the shell.xaml window. In the modules which are class libraries I have the initialize function setting the views and viewmodels. I have only two regions in this application the navigation and the workspace. I have 2 buttons on the navigation and they change the view in the workspace. The workspace views are in their own modules. So at this point each workspace view has its own class library module. In a big application this seems unreasonable of having each view have its own class library. I wanted to know how can I have multiple views and viewmodels in one class library and swap them in and out. If you have a good step by step tutorial that would be great.
I know this is 4 years late, but I ll give an answer anyway.
First thing you do is add the Views you need to the Views folder and add their corresponding ViewModels in the ViewModels folder so that you have a structure as described below :
ViewModels
ViewModelA
ViewModelB
Views
ViewA
ViewB
The ViewModels and their corresponding view could look something like this :
ViewModelA
using System;
namespace SomeApp.DemoModule.ViewModels
{
public class ViewModelA : INavigationAware
{
public ViewModelA(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
{
this.container = container;
this.regionManager = regionManager;
this.eventAggregator = eventAggregator;
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//Do stuff here
//For navigation back
navigationService = navigationContext.NavigationService;
}
#region Executes
/// <summary>
/// Command when ViewB button clicked
/// </summary>
public void Execute_ViewBCommand()
{
regionManager.RequestNavigate("REGIONNAME_HERE", new Uri("ViewB", UriKind.Relative));
}
ViewA
<UserControl x:Class="DemoModule.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ViewInjection.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Content="VIEWB" FontSize="38" Command="{Binding ViewBCommand}"></Button>
</Grid>
ViewA.Xaml.cs
namespace SomeApp.DemoModule.Views
{
/// <summary>
/// Interaction logic for ViewA.xaml
/// </summary>
public partial class ViewA : UserControl
{
public ViewA(ViewModelA model)
{
InitializeComponent();
this.DataContext = model;
}
}
}
ViewModelB
using System;
namespace SomeApp.DemoModule.ViewModels
{
public class ViewModelB : INavigationAware
{
public ViewModelB(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
{
this.container = container;
this.regionManager = regionManager;
this.eventAggregator = eventAggregator;
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//Do stuff here
//For navigation back
navigationService = navigationContext.NavigationService;
}
#region Executes
/// <summary>
/// Command when ViewA button clicked
/// </summary>
public void Execute_ViewACommand()
{
regionManager.RequestNavigate("REGIONNAME_HERE", new Uri("ViewA", UriKind.Relative));
}
ViewB
<UserControl x:Class="DemoModule.Views.ViewB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Content="VIEWA" FontSize="38" Command="{Binding ViewACommand}"></Button>
</Grid>
ViewB.Xaml.cs
namespace SomeApp.DemoModule.Views
{
/// <summary>
/// Interaction logic for ViewB.xaml
/// </summary>
public partial class ViewB : UserControl
{
public ViewB(ViewModelB model)
{
InitializeComponent();
this.DataContext = model;
}
}
}
You can register your views in several ways, we use a DemoModuleInit class in the project root that registers the view :
DemoModuleInit
public class DemoModuleInit : IModule
{
private IRegionManager regionManager;
/// <summary>
/// Bind your interfaces, subscribe to events, do stuff that needs to be done on intialization of module
/// </summary>
public void OnInitialized(IContainerProvider containerProvider)
{
// Setup Event listeners etc...
regionManager = containerProvider.Resolve<IRegionManager>();
}
/// <summary>
/// Register your views for this Module
/// </summary>
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<ViewB>();
}
If implemented correctly you should be able to navigate from ViewA to ViewB and back within the same module
For more information on Prism check out :
Prsim on GitHub
you can have as many views in your module as you need. You just need to navigate through them. To do so, you need to register them and then from each of the viewmodel you can request navigate to another view from your regionmanager. Have a look here prism navigation
I have been learning MVVM / WPF and have gone through the tutorial here.
I have created a working application using this methodology but now, on a new project, I cannot get the Dependency Injection to work.
When I run this project I get an empty MainWindow without the CompanyView injected. I have double and tripled checked everything between the project that works and this one that doesn't and cannot find the reason for CompanyView not being injected. I have also tried cleaning the solution and restarting VS to no avail. Hopefully someone can see what I am missing.
I have the following file:
App.xaml.cs (using base.OnStartup() instead of StartupUri in App.xaml)
namespace SidekickAdmin
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
var viewModel = new MainWindowViewModel();
window.DataContext = viewModel;
window.Show();
}
}
}
MainWindowViewModel.cs
namespace SidekickAdmin.ViewModel
{
class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
CompanyViewModel companyViewModel = new CompanyViewModel(_repository);
this.ViewModels.Add(companyViewModel);
}
ObservableCollection<ViewModelBase> _viewModels;
ObservableCollection<ViewModelBase> ViewModels
{
get
{
if (_viewModels == null)
{
_viewModels = new ObservableCollection<ViewModelBase>();
}
return _viewModels;
}
}
}
}
MainWindowView.xaml
<Window x:Class="SidekickAdmin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:SidekickAdmin.ViewModel"
xmlns:vw="clr-namespace:SidekickAdmin.View"
Title="Sidekick Admin" SizeToContent="WidthAndHeight">
<!-- Typically done in a resources dictionary -->
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>
<StackPanel>
<ItemsControl ItemsSource="{Binding ViewModel}" Margin="3" />
</StackPanel>
</Window>
MainWindowResources.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:SidekickAdmin.ViewModel"
xmlns:vw="clr-namespace:SidekickAdmin.View">
<DataTemplate DataType="{x:Type vm:CompanyViewModel}">
<vw:CompanyView />
</DataTemplate>
</ResourceDictionary>
CompanyViewModel.cs (not really used yet as I am still trying to just get the view to appear)
namespace SidekickAdmin.ViewModel
{
class CompanyViewModel : ViewModelBase
{
readonly GenericRepository _repository;
#region Getters & Setters
public ObservableCollection<Company> AllCompanies
{
get;
private set;
}
#endregion
#region Constructors
public CompanyViewModel(GenericRepository repository)
{
if (repository == null)
{
throw new ArgumentNullException("repository");
}
_repository = repository;
this.AllCompanies = new ObservableCollection<Company>(_repository.GetAll<Company>());
}
#endregion
protected override void OnDispose()
{
this.AllCompanies.Clear();
}
}
}
CompanyView.xaml
<UserControl x:Class="SidekickAdmin.View.CompanyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="300" Width="300">
<StackPanel>
<TextBlock>You say Hello</TextBlock>
<TextBlock>And I say Goodbye</TextBlock>
<TextBlock>Hello, Hello</TextBlock>
</StackPanel>
</UserControl>
Lester's comment is right... you are binding to a ViewModel property which does not exist - MainWindowViewModel has a ViewModels property though. The s is important
Besides what #Robert Levy has wrote, the error you are making is that your ViewModels property is private, make it public and it should work fine.
#RobertLevy and #dmusial are correct. You need to make your reference to ViewModels in your XAML plural, to match the property name in your C# code. Also, the property should be public, so the View can see it.
I have two UserControls (uc1 and uc2) loading into a third UserControl (shell). Shell has two properties, uc1 and uc2, of type UserControl1 and UserControl2, and each have a DependencyProperty registered to their own classes called IsDirty:
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register("IsDirty", typeof (bool), typeof (UserControl1));
public bool IsDirty
{
get { return (bool) GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
(same code for UserControl2)
Shell has TextBlocks bound to the IsDirty properties:
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
<TextBlock Text="{Binding ElementName=shell, Path=Uc2.IsDirty}"/>
When I change the values of IsDirty in uc1 and uc2, Shell never gets notified. What am I missing? UserControl is descendant of DependencyObject...
The same behavior occurs if I have regular properties notifying changes via INotifyPropertyChanged.
If I raise a routed event from uc1 and uc2, bubbling up to Shell, then I can catch the Dirty value and everything works, but I shouldn't have to do that, should I?
Thanks
Edit: The answer is to raise property changed event on the Uc1 and Uc2 properties or make them DPs.
I tried reproducing your problem using a simple setup, and it works fine for me. I'm not sure though if this setup is correct enough to replicate your situation. Anyway, I'm posting it just in case. It might be helpful:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
x:Name="shell"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Click="Button_Click">Click</Button>
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
</StackPanel>
</Window>
Code-Behind:
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MyUserControl uc1 = new MyUserControl();
public MyUserControl Uc1
{
get { return this.uc1; }
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.uc1.IsDirty = !this.uc1.IsDirty;
}
}
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
}
public bool IsDirty
{
get { return (bool)GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
// Using a DependencyProperty as the backing store for IsDirty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDirtyProperty =
DependencyProperty.Register("IsDirty", typeof(bool), typeof(UserControl), new UIPropertyMetadata(false));
}
}
Karmicpuppet's answer works well. However it didn't solve my problem because Shell is also of type UserControl. For it to work I needed to raise the property changed on Uc1 and Uc2. When I declared them as DependencyProperties all worked as expected. Duh!
I'm just playing around with WPF and MVVM, and I have made a simple app that displays a Rectangle that changes color whenever Network availability changes.
But when that happens, I get this error: Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.
Code
XAML
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="600">
<DockPanel LastChildFill="True">
<Rectangle x:Name="networkStatusRectangle" Width="200" Height="200" Fill="{Binding NetworkStatusColor}" />
</DockPanel>
</Window>
Code-behind
using System.Windows;
using WpfApplication1.ViewModels;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new NetworkViewModel();
}
}
}
ViewModel
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Windows.Media;
namespace WpfApplication1.ViewModels
{
public class NetworkViewModel : INotifyPropertyChanged
{
private Brush _NetworkStatusColor;
public Brush NetworkStatusColor
{
get { return _NetworkStatusColor; }
set
{
_NetworkStatusColor = value;
NotifyOfPropertyChange("NetworkStatusColor");
}
}
public NetworkViewModel()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
protected void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
this.NetworkStatusColor = new SolidColorBrush(Colors.Green);
}
else
{
this.NetworkStatusColor = new SolidColorBrush(Colors.Red);
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void NotifyOfPropertyChange(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I assume that I should change the NetworkStatusColor property by invoking something?
You assume correctly. It's the Dispatcher class and the .Invoke method you want to take a look at.
Something a bit like this:
if (this.Dispatcher.Thread != Thread.CurrentThread)
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(...your method...), any, params, here);
return
}
There's an MSDN article here with some more info.
With MVVM you have a couple of options when dealing with dispatching. Either you can send some kind of message to your view to have it invoke the operation for you, or you can create some kind of abstract dispatcher service that you are able to easily mock.
Take a look at the MVVM Light toolkit, as it includes a simple dispatcher-service you can use/copy.