Accessing unity container in view model class - wpf

I have a shell which looks like toolbar and defines my main region (a wrap panel). What I need to do is be able to add widgets to the shell and when a widget is clicked, a new window (view) is opened. Below is what I have so far:
I created a module class which adds a view to the main region:
public class MyModule : IModule
{
protected IUnityContainer Container { get; private set; }
public MyModule(IUnityContainer container)
{
Container = container.CreateChildContainer();
}
public void Initialize()
{
var regionManager = Container.Resolve<IRegionManager>();
MyModuleView myModuleView = new MyModuleView();
regionManager.Regions["MainRegion"].Add(myModuleView);
}
}
Here is the content of MyModuleView:
<Grid>
<Grid.DataContext>
<vm:MyModuleVM/>
</Grid.DataContext>
<Button Content="My Module" Foreground="White" FontWeight="Bold" Command="{Binding Path=LaunchCommand}">
</Button>
</Grid>
The view model, MyModuleVM:
class MyModuleVM : ObservableObject
{
protected IUnityContainer Container { get; private set; }
public MyModuleVM()
{
}
RelayCommand _launchCommand;
public ICommand LaunchCommand
{
get
{
if (_launchCommand == null)
{
_launchCommand = new RelayCommand(() => this.LaunchTestView(),
() => this.CanLaunchTestView());
}
return _launchCommand;
}
}
private void LaunchTestView()
{
TestView view = new TestView();
view.Title = "Test View";
var regionManager = Container.Resolve<IRegionManager>();
regionManager.Regions["MyWindowRegion"].Add(view);
}
private bool CanLaunchTestView()
{
return true;
}
}
So my plan was as follows:
Create the class that implements
IModule (MyModule) and have it load a
view (MyModuleView) into the shell
when initialized
Create a view model for the module
(MyModuleVM) and set it as the
DataContext of the view displayed in
the shell
MyModuleVM contains a command that a
button in MyModuleView binds to.
When the button is clicked the
command is triggered
Now, here is where I am stuck. Using
a WindowRegionAdapter (an adapter
that helps to create views in
separate windows) I wanted to create
and display a new view. As seen in
MyModuleVM, LaunchTestView needs
access to the container in order to
add the view to a region. How am I
supposed to get to the container?
Besides my specific question about accessing the container, how is my overall strategy of adding "widgets" to a toolbar shell and launching
views when they are clicked? Am I comlpetely off track here when it comes to MVVM with Prism?
Thanks guys.

You can get the container injected through constructor or property injection. To do that, the ViewModel instance must be resolved by the container, or the BuildUp method should be called after it has been instantiated.
I hope this helps.
Thanks,
Damian

Related

OnNavigatedTo is not called in TabControl when selecting tab when using PRISM regions

I have registered my views for the TabControl with Region manager and views are shown properly when tab is selected.
The problem is that when I select new tab item OnNavigatedTo is not called for that view or its view model.
I'm using PRISM 6.3
UPDATE
ViewModel
`public class ValuationViewModel : IViewModel, INavigationAware
{
private IRegionManager _regionManager;
public string Title { get; set; }
public ValuationViewModel(IRegionManager regionManager)
{
Title = "PERFORM VALUATION";
_regionManager = regionManager;
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
}`
View
`public partial class ValuationView : UserControl, IView
{
private IRegionManager _regionManager;
public ValuationView(ValuationViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
}
public IViewModel ViewModel
{
get
{
return (IViewModel)DataContext;
}
set
{
DataContext = value;
}
}
}`
Without code, nobody can give you the correct answer.
Its probably the best, if you show us your ViewModel for your "TabItem" View.
Assuming you registered your view and set your ViewModel in DataContext correctly, it could be possible that forget just a simple thing.
To manage your problem make sure you implemented the following things correctly:
Create a region for your TabControl
Register your view in that region
Make sure the DataContext is correctly set to your ViewModel
Make sure your ViewModel implemented INavigationAware
Update 1:
After testing a lot I found a simple answer unfortunately:
Members of INavigationAware (OnNavigatedTo, IsNavigationTarget & OnNavigatedFrom) are called when the NavigationService is navigating.
They aren't if you click on the TabItemHeader.
To solve your problem you have several options.
One option is to start a navigation request when the user click on the TabItemHeader ( bad approach).
In my opinion you should use the IActiveAware Interface ( https://msdn.microsoft.com/en-us/library/microsoft.practices.prism.iactiveaware(v=pandp.50).aspx).
It will solve your problem, because the navigation via RegionManager and the clicking on the TabItemHeader results in the same: INavigationAware.IsActive = true.
Now you are able to detect when your tab is shown or not and react.

TabControl region, How to pass parameter to child region? WPF - Prism

I'm using prism regions in order to create dynamic TabControl. But I'm having a problem passing the object from TabItem (parent view) to its child regions.
The below is the code I'm using to build the TabControl.
Shell:
xaml
<ContentControl regions:RegionManager.RegionName="ShellProjectRegion" />
ShellViewModel
regionManager.RegisterViewWithRegion(ShellProjectRegion, typeof(ProjectTabView));
ProjectTabView:
xaml
<TabControl regions:RegionManager.RegionName="ProjectTabRegion">
ProjectTabViewModel
container.RegisterType<object, ProjectView>(typeof(ProjectView).FullName);
ProjectView:
xaml
<Grid>
<ContentControl regions:RegionManager.RegionName="ProjectExplorerRegion"
regions:RegionManager.RegionContext="{Binding}" />
</Grid>
ProjectViewModel
public class ProjectViewModel : BindableBase, INavigationAware, IActiveAware {
private ProjectItem _project;
public ProjectItem Project {
get { return _project; }
set { SetProperty(ref _project, value); }
}
public ProjectViewModel(IRegionManager regionManager) {
regionManager.RegisterViewWithRegion("ProjectExplorerRegion", typeof(ProjectExplorerView));
}
public void OnNavigatedTo(NavigationContext navigationContext) {
Project = (ProjectItem)navigationContext.Parameters["project"];
}
}
ProjectExplorerView:
xaml.cs
public ProjectExplorerView(IUnityContainer container) {
InitializeComponent();
var vm = container.Resolve<ProjectExplorerViewModel>();
RegionContext.GetObservableContext(this).PropertyChanged += (s, e) => {
var context = (ObservableObject<object>)s;
var projectVm = (ProjectViewModel)context.Value;
vm.ParentProjectInfo = projectVm.Project.ProjectInfo;
};
DataContext = vm;
}
Note: Please note that in the last piece of code inside the ProjectExplorerView.xaml.cs the view constructor gets called multiple times each time new Tab is created. when tracing the code, the context variable gets null sometimes, and sometimes has the right value, which is the project I want to pass. but the it's always null at the end of calling the constructor.
So I'm not sure if this is the right way to do it, but it works.
First I've removed regionManager.RegisterViewWithRegion("ProjectExplorerRegion", typeof(ProjectExplorerView)); from ProjectViewModel to ShellViewModel, this was causing the view to be called multiple times as I have mentioned at the end of my question.
Second update the ParentProjectInfo implementation to use INotifyPropertyChanged, and inside the property setter, update what needs to be automatically updated.

Caliburn.Micro: Communication between user controls

I'm building my first Caliburn WPF application, and I find myself in the following problem.
I have a parent view, with loads two user controls: Search & Results. When the search button is clicked on the Search user control, I wan't to load the results in the results user control.
Parent View:
<ContentControl x:Name="SearchViewModel"/>
<ContentControl x:Name="ResultsViewModel"/>
Parent VM
[Export(typeof(IMainViewModel))]
public class ParentViewModel : Screen, IMainViewModel{
public SearchViewModel SearchViewModel { get; set; }
public ResultsViewModel ResultsViewModel { get; set; }
public ParentViewModel()
{
SearchViewModel = new SearchViewModel();
ResultsViewModel = new ResultsViewModel();
}
}
Search View
<TextBox x:Name="Term"/>
<Button Content="Search" x:Name="Search"/>
Search VM
public class SearchViewModel : PropertyChangedBase
{
private string _term;
public string Term
{
get { return _term; }
set
{
_instrumentId = value;
NotifyOfPropertyChange(() => _term);
}
}
public void Search()
{
//Call WCF Service
//Send results to results user control?
}
}
So actually how can i pass or access data/methods between different user controls / view models with caliburn micro?
You can use events via the Caliburn Micro Event Aggregator. You can publish an event in one viewmodel and subscribe that event in the other. This keep the model decoupled - the only coupling is done by the event itself-, in which you can store the data to transfer.

Prism: Showing a dialog window through two modules

I have two Prism modules.
I want one of them register a window and the other one show this window using the "Show Dialog" mode.
How can it be done (if it can be done)?
Yes, it can be done. This is rough procedure:
Declare interface for this View in your "Infrastructure" project
public interface IMyDialogWindow
{
}
[Export] class that implements this interface in your module
[Export(typeof(IMyDialogWindow))]
public class MyClassInModuleA : IMyDialogWindow
{
}
[Import] this class in other module and use it for Dialog
[Import]
public IMyDialogWindow PropertyInModuleB
Well. I think I solved it by following this tip. But I don't know if it was the best solution.
I just created a window on my Shell project. This window is the one that will be popped up as a dialog window.
Here is its code:
Popup.xaml:
<Window x:Class="TryERP2.Shell.Views.Popup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Popup" Height="315" Width="411"
xmlns:prism="http://www.codeplex.com/prism">
<Grid>
<ContentControl x:Name="DialogRegion" Grid.Row="1" prism:RegionManager.RegionName="DialogRegion" />
</Grid>
</Window>
Popup.xaml.cs:
public partial class Popup : Window
{
private static Popup popup;
private Popup(IRegionManager regionManager)
{
InitializeComponent();
RegionManager.SetRegionManager(this, regionManager);
}
//Using the singleton pattern
public static Popup getPopup(IRegionManager regionManager)
{
if (popup == null)
popup = new Popup(regionManager);
return popup;
}
}
And, finally, when I want to show the dialog (in a Command which is in a module), I just instantiate it and inform what's the RegionManager:
private void showDialog()
{
// Acquiring the RegionManager
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
// Getting the Popup object
Popup p = Popup.getPopup(regionManager);
// Looking for the view I want to show in the dialog
var x = new Uri("MyView", UriKind.Relative);
// Changing the view of the DialogRegion (which is within the Popup)
regionManager.RequestNavigate("DialogRegion", x);
// Showing the dialog
p.ShowDialog();
}

Loading Views into ContentControl and changing their properties by clicking buttons

I have a mvvm(model view viewmodel) silverlight application that has several views that need to be loaded into ContentControls (i made it all in expression blend). What i dont know how to do is, for example, to load one view (user control) in one content control by clicking a button from another view that is in another content control. To make it easier to understand the problem, i need to do something similar to this:
http://www.codeproject.com/KB/silverlight/BlendableVMCom.aspx
with that difference that child1 and child2 are supposed to be loaded into theirown content controls by clicking Call child1 or call child2 buttons.
and example would be appreciated. Thanks in advance!
This example is very simplified, but I think you now how to adjust it to your application.
The main view:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="commandsView">
<Button Content="Call view 1" Command="{Binding CallView1Command}" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" />
</Border>
<Border x:Name="displayedView" Grid.Column="1">
<ContentControl Content="{Binding CurrentView}" />
</Border>
</Grid>
I haven't created separated views as user controls, here are just borders, which can be replaced by real views.
Different view models for different views in code behind:
this.commandsView.DataContext = new CommandsViewModel();
this.displayedView.DataContext = new DisplayedViewModel();
First view model conains the command which sends the message to another view model:
public class CommandsViewModel
{
public CommandsViewModel()
{
this.CallView1Command = new RelayCommand(() =>
Messenger.Default.Send<View1Message>(new View1Message()));
}
public RelayCommand CallView1Command { get; set; }
}
public class View1Message : MessageBase
{
}
To make this example work, download the MVVM Light library.
The second view model receive the message and creates a view for its property:
public class DisplayedViewModel : ViewModelBase
{
public DisplayedViewModel()
{
Messenger.Default.Register<View1Message>(this, obj =>
this.CurrentView = new TextBlock { Text = "Pressed the button 1 and now here is the view 1" });
}
private object currentView;
public object CurrentView
{
get { return currentView; }
set
{
currentView = value;
RaisePropertyChanged("CurrentView");
}
}
}
Again, it is possible to use clr object instead of controls and apply data templates in xaml, but there will not be enough space to provide all the resulting code.
So that is all, the main idea is a some kind of event aggregator, which is the Messenger class in this particular case.
Without the MVVM Light it will require more code:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
var events = new GlobalEvents();
this.commandsView.DataContext = new CommandsViewModel(events);
this.displayedView.DataContext = new DisplayedViewModel(events);
}
}
public class GlobalEvents
{
public event EventHandler View1Event = delegate { };
public void RaiseView1Event()
{
View1Event(this, EventArgs.Empty);
}
}
/// <summary>
/// Commands which call different views
/// </summary>
public class CommandsViewModel
{
public CommandsViewModel(GlobalEvents globalEvents)
{
this.CallView1Command = new DelegateCommand(globalEvents.RaiseView1Event);
}
public DelegateCommand CallView1Command { get; set; }
}
/// <summary>
/// Model where views are changed and then displayed
/// </summary>
public class DisplayedViewModel : INotifyPropertyChanged
{
public DisplayedViewModel(GlobalEvents globalEvents)
{
globalEvents.View1Event += (s,e) =>
this.CurrentView = new TextBlock { Text = "Pressed the button 1 and now here is the view 1" };
}
private object currentView;
public object CurrentView
{
get { return currentView; }
set
{
currentView = value;
RaisePropertyChanged("CurrentView");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
In this example you must change the DelegateCommand class for something different. Other code will work for everyone.
It sounds like you might be trying to do some sort of navigation. If that's true, check out the Silverlight navigation framework.

Resources