Search code examples
c#inversion-of-controlprismioc-container

Prism: RegisterViewWithRegion does not find public contructor


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.

enter image description here

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!

Update 1

DBInteraction.cs had some problem (see solution)


My code

DBInteraction.cs (Update 1)

public class DBInteraction : IDBInteraction
{
    LinqToTaskPlanSqlDataContext dataContext;

    public DBInteraction()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["TaskPlanConnectionString"].ConnectionString;
        dataContext = new LinqToTaskPlanSqlDataContext(connectionString);
    }
}

Shell.xaml

<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>

App.xaml.cs

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>();
    }
}

DataServiceModule.cs

public class DataServiceModule : IModule
{
    public DataServiceModule()
    {

    }

    public void OnInitialized(IContainerProvider containerProvider)
    {

    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<IDBInteraction, DBInteraction>(typeof(DBInteraction).FullName);
    }        
}

UIModule.cs

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>();
    }
}

ShowStringView.xaml

<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>

ShowStringViewModel

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);
    }
}

Solution

  • See Update1

    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.

    app.config file in wrong place