I have some trouble with the regionManager.RegisterViewWithRegion
methode.
I use Prism.Unity and Prism.Wpf (both v7.2.0.1367)
When I want to register the View ShowStringView
(I created this View just for getting started with prism...) in the UIModule.cs
, I always get Set property 'Prism.Mvvm.ViewModelLocator.AutoWireViewModel' threw an exception.' Line number '7' and line position '14'.
inside of the ShowStringView.xaml
.
When I swap the construtor in ShowStringViewModel.cs
, which has the IDBInteraction as parameter, with an constructor without parameters it works. (see below)
I'm sure that there's something I don't think of or I use it the wrong way...
Would be super nice if someone could help me.
Thanks in advance!
DBInteraction.cs had some problem (see solution)
public class DBInteraction : IDBInteraction
{
LinqToTaskPlanSqlDataContext dataContext;
public DBInteraction()
{
string connectionString = ConfigurationManager.ConnectionStrings["TaskPlanConnectionString"].ConnectionString;
dataContext = new LinqToTaskPlanSqlDataContext(connectionString);
}
}
<Window x:Class="TP.Client.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TP.Client"
xmlns:prism="http://prismlibrary.com/"
xmlns:region="clr-namespace:TP.Common;assembly=TP.Common"
mc:Ignorable="d"
Title="Shell" Height="450" Width="800">
<Grid>
<ContentControl prism:RegionManager.RegionName="{x:Static region:RegionNames.ShowStringRegion}" />
</Grid>
</Window>
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<DataServiceModule>();
moduleCatalog.AddModule<UIModule>();
}
}
public class DataServiceModule : IModule
{
public DataServiceModule()
{
}
public void OnInitialized(IContainerProvider containerProvider)
{
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IDBInteraction, DBInteraction>(typeof(DBInteraction).FullName);
}
}
public class UIModule : IModule
{
IRegionManager _regionManager;
public UIModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RegisterViewWithRegion(RegionNames.ShowStringRegion, typeof(ShowStringView));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
ViewModelLocationProvider.Register<ShowStringView, ShowStringViewModel>();
}
}
<UserControl x:Class="TP.UI.View.ShowStringView"
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:TP.UI.View"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d">
<Grid>
<TextBox Text="{Binding Content}" Height="20" Width="150" BorderBrush="Black"/>
</Grid>
public class ShowStringViewModel : BindableBase
{
IDBInteraction _dBInteraction;
public ShowStringViewModel()
{
Content = "Hallo";
}
// It works when this ctor is comment-out
public ShowStringViewModel(IDBInteraction dBInteraction)
{
_dBInteraction = dBInteraction;
Content = "Hallo";
}
private string _content;
public string Content
{
get => _content;
set => SetProperty(ref _content, value);
}
}
Inside of the DBInteraction constructor I do read the connectionString from the app.config
file.
The problem was that this file was not in my Startup-Project, it was in an ClassLibrary-Project.
I just moved the app.config
file from ClassLibrary to Startup (drag&drop in MSVS Solution Explorer) and everything worked.
That's why the DBInteraction just could not be created -> there always was an exception inside the constructor.