Prism AutoWireViewModel与MEF兼容吗?

时间:2016-06-13 04:17:25

标签: c# wpf mvvm prism prism-6

我正在尝试将AutoWireViewModelMefBootstrapper结合使用,以在各种模块中的视图之间导航。但是我得到了一个例外:

  

设置属性' Prism.Mvvm.ViewModelLocator.AutoWireViewModel'扔了一个   例外

此行代码的异常指向: 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>

查看:

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

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

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

视图模型:

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

外壳:

[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

1 个答案:

答案 0 :(得分:1)

LoginFormViewModel需要Export属性。

 [Export]
 public class LoginFormViewModel ...

并添加System.ComponentModel.Compositon命名空间。