【问题标题】:MEF import does not work with a Shell that has more than one active screenMEF 导入不适用于具有多个活动屏幕的 Shell
【发布时间】:2011-10-16 16:44:18
【问题描述】:

您好,我使用 WPF/Calibur Micro/MEF。我尝试在 shell 中有更多活动屏幕。首先我定义屏幕 - 用户控件。

  1. 屏幕 - 查看模型
public interface IProjectsViewModel
{

}
[Export(typeof(IProjectsViewModel))]
public class ProjectsViewModel:Screen,
    IProjectsViewModel
{

}

2.屏幕-视图模型

public interface IProjectInfoViewModel
{

}

[Export(typeof(IProjectInfoViewModel))]
public class ProjectInfoViewModel :Screen, 
    IProjectInfoViewModel
{
    [Import]
    internal IMessageBox MsgBox { get; set; }

    public void BtnClick()
    {
        MsgBox.ShowInfo("Btn click",string.Empty);
    }
}

如果用户点击显示消息框的按钮,我只有一个按钮。

没有最重要的部分外壳。 Shell 是 WPF 窗口。

查看:

<Window x:Class="CaliburnSkelet.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Views="clr-namespace:CaliburnSkelet.Views" 
        xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
        Title="ShellView" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <!--<ContentControl x:Name="Projects" Grid.Column="0"/>
        <ContentControl x:Name="ProjectInfo" Grid.Column="1"/>-->
        <Views:ProjectsView  cal:View.Model="{Binding Projects}" 
                             Grid.Column="0"/>
        <Views:ProjectInfoView cal:View.Model="{Binding ProjectInfo}"
                               Grid.Column="1"/>
    </Grid>
</Window>

ShellViewModel:

public interface IShellViewModel :IScreen
{
    ProjectInfoViewModel ProjectInfo { get; set; }
    ProjectsViewModel Project { get; set; }
}

[Export(typeof(IShellViewModel))]
public class ShellViewModel:Conductor<IScreen>.Collection.AllActive,
    IShellViewModel, IPartImportsSatisfiedNotification

{
    [Import]
    public ProjectInfoViewModel ProjectInfo { get; set; }

    [Import]
    public ProjectsViewModel Project { get; set; }

    public void OnImportsSatisfied()
    {
    }
}

如果我尝试编译这段代码,我会得到错误:

找不到任何合同实例 CaliburnSkelet.ViewModels.IShellViewModel。

堆栈跟踪:

  at CaliburnSkelet.BootStraper.MefBootStrapper.GetInstance(Type serviceType, String key) in E:\C# PROJECTS\CaliburnSkelet\CaliburnSkelet\BootStraper\MefBootStrapper.cs:line 69
   at Caliburn.Micro.Bootstrapper.DisplayRootViewFor(Type viewModelType)
   at Caliburn.Micro.Bootstrapper`1.OnStartup(Object sender, StartupEventArgs e)
   at System.Windows.Application.OnStartup(StartupEventArgs e)
   at System.Windows.Application.<.ctor>b__1(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

我尝试进行测试并更改属性 shell 视图模型类。

[Export(typeof(IShellViewModel))]
public class ShellViewModel:Conductor<IScreen>.Collection.AllActive,
    IShellViewModel, IPartImportsSatisfiedNotification

{
    //[Import]
    public ProjectInfoViewModel ProjectInfo { get; set; }

    //[Import]
    public ProjectsViewModel Project { get; set; }

    public void OnImportsSatisfied()
    {
        ProjectInfo=new ProjectInfoViewModel();
        Project=new ProjectsViewModel();
    }
}

我没有使用 MEF 将视图模型类导入 shell 视图模型,但我在方法 OnImportsSatisfied 中创建了新实例。

应用程序运行,但如果我点击按钮变量 MsgBox 为空。

ProjectInfoViewModel 类的代码:

        [Import]
        internal IMessageBox MsgBox { get; set; }

        public void BtnClick()
        {
//MsgBox is null
            MsgBox.ShowInfo("Btn click",string.Empty);
        }

哪里有问题?

在这里? Conductor.Collection.AllActive

【问题讨论】:

    标签: wpf shell screen mef caliburn.micro


    【解决方案1】:

    在您的 ShellViewModel 类型上,ProjectInfoProject 属性应该是 IProjectInfoViewModelIProjectsViewModel 类型,因为这些是您导出的类型,您应该使用这些接口。

    【讨论】:

    • 非常感谢,愚蠢的问题...我将视图模型类导出为接口...你是对的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2019-07-22
    相关资源
    最近更新 更多