0

I am trying to combine AutoWireViewModel with MefBootstrapper to navigate between Views in various Modules. However I get an exception:

Set property 'Prism.Mvvm.ViewModelLocator.AutoWireViewModel' threw an exception

The exception points to this line of code: xmlns:prism="http://www.codeplex.com/prism"

XAML:

<UserControl x:Class="Login.Views.LoginForm"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:prism="http://www.codeplex.com/prism"
            prism:ViewModelLocator.AutoWireViewModel="True" Width="350">

    <Grid x:Name="LayoutRoot">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF7B97D4" />
                <GradientStop Color="#FF244C8D" Offset="1" />
            </LinearGradientBrush>
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Text="Login" TextWrapping="Wrap" Grid.RowSpan="1" Grid.ColumnSpan="2" FontSize="18" Foreground="#FF2F3806" Margin="8,8,8,8" />

        <Label Grid.Row="1" Margin="10,10,10,10">username:</Label>
        <TextBox x:Name="txtUsername" Text="{Binding UserName}" Grid.Row="1" Grid.Column="1" Margin="10,10,10,10" VerticalContentAlignment="Center"></TextBox>
        <Label Grid.Row="2" Margin="10">password:</Label>
        <PasswordBox x:Name="txtPassword"  Grid.Row="2" Grid.Column="1" Margin="10,10,10,10" VerticalContentAlignment="Center" />
        <Button x:Name="btnLogin" Grid.Row="3" Grid.RowSpan="1" Grid.ColumnSpan="2" Margin="10,10,10,10" Content="Log In" FontWeight="Bold" />
        <TextBlock x:Name="txtMsg" Grid.Row="4"  Grid.RowSpan="1" Grid.ColumnSpan="2" Margin="10,10,10,10" />

    </Grid>

</UserControl>

View:

using System.ComponentModel.Composition;
using System.Windows.Controls;

namespace Login.Views
{
    [Export("LoginForm")]
    public partial class LoginForm : UserControl
    {

        public LoginForm()
        {
            InitializeComponent();
        }
    }
}

ViewModel:

using Prism.Mvvm;

namespace Login.ViewModels
{
    public class LoginFormViewModel : BindableBase
    {

        private string _username = "unamegoeshere";
        public string UserName
        {
            get { return _username; }
            set { SetProperty(ref _username, value); }
        }
    }
}

Shell:

[Export]
public partial class Shell : Window, IPartImportsSatisfiedNotification
{
    private const string InitialModuleName = "LoginModule";
    private static Uri InitialViewUri = new Uri("/LoginForm", UriKind.Relative);
    public Shell()
    {
        InitializeComponent();
    }

[Import(AllowRecomposition = false)]
public IModuleManager ModuleManager;

[Import(AllowRecomposition = false)]
public IRegionManager RegionManager;

public void OnImportsSatisfied()
{
    this.ModuleManager.LoadModuleCompleted +=
        (s, e) =>
        {
            if (e.ModuleInfo.ModuleName == InitialModuleName)
            {
                this.RegionManager.RequestNavigate(
                    RegionNames.MainContentRegion,
                    InitialViewUri);
            }
        };
}

Screenshot of error

Cœur
  • 37,241
  • 25
  • 195
  • 267
keeg
  • 3,990
  • 8
  • 49
  • 97
  • What version of Prism are you working with? I cannot repro this with 6.1. – R. Richards Jun 15 '16 at 17:10
  • Does the form appear for you? The error doesn't happen unless the LoginForm is actually viewable.. – keeg Jun 15 '16 at 17:21
  • Yes, I am getting the form to show, but no error. Works every time. – R. Richards Jun 15 '16 at 17:32
  • so strange... could it be a reference issue? – keeg Jun 15 '16 at 17:42
  • Good question, not sure. I did pull down all I needed to try and recreate this fresh today. I even played around with the more recent xmlns for Prism: (`xmlns:prism="http://prismlibrary.com/"`), but that made no difference. Stumped. – R. Richards Jun 15 '16 at 17:47
  • Yes I played around with `xmlns:prism` ref too, made not diff... Thinking maybe something just got corrupted, maybe I need to start a new – keeg Jun 15 '16 at 17:51
  • I have seen this type of error with XAML and VS before. To clear it, I end up closing VS and reopening the solution. The code looks right, I can see without a doubt that it is correct, but VS is choking. It's frustrating. – R. Richards Jun 15 '16 at 18:14
  • yeah been in and out of the solution, still an error... – keeg Jun 15 '16 at 18:51
  • rebuilt the whole thing and the same error! Argh. Would you be willing to post your source somewhere so I could compare? – keeg Jun 16 '16 at 04:21
  • I don't have a good place to put the code for you to pick up. If you are will to share your email address with me, I can send it to you. Or, do you have a place where I can drop it? – R. Richards Jun 16 '16 at 11:07
  • The email bounced back – R. Richards Jun 16 '16 at 17:40
  • The zip file is only 47k. The email address seems wrong. – R. Richards Jun 16 '16 at 17:43
  • Keeps bouncing back, even when I replied. Well, with the attachment. I wonder if it is the code file in the zip that causing this. – R. Richards Jun 16 '16 at 17:49
  • Keep an eye out for this in your junk folder, I think I got it to go through. – R. Richards Jun 16 '16 at 17:59
  • 1
    I've been on several projects that used MEF. Even time we ended up ripping it out for one reason or another. MEF is meant to solve Visual Studio's problems; it isn't appropriate for most applications. – Jonathan Allen Jun 16 '16 at 23:22
  • To chime in Jonathan's comment: MEF is not an IoC container. It's useful in certain (limited) scenario's but quite often misused as IoC container. – Bart Jun 17 '16 at 11:37
  • What would be a better solution for being able to load and unload views from various modules into regions? – keeg Jun 17 '16 at 13:13

1 Answers1

1

The LoginFormViewModel needs the Export attribute.

 [Export]
 public class LoginFormViewModel ...

And add the System.ComponentModel.Compositon namespace, too.

R. Richards
  • 24,603
  • 10
  • 64
  • 64