MEF 导入不适用于具有多个活动屏幕的 Shell

发布于 2024-12-10 05:03:26 字数 4025 浏览 0 评论 0原文

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

  1. 屏幕 - 查看模型
公共接口IProjectsViewModel
{

}
[导出(类型(IProjectsViewModel))]
公共类 ProjectsViewModel:Screen,
    项目视图模型
{

}

2.屏幕视图模型

公共接口IProjectInfoViewModel
{

}

[导出(typeof(IProjectInfoViewModel))]
公共类 ProjectInfoViewModel :屏幕, 
    项目信息视图模型
{
    [进口]
    内部 IMessageBox MsgBox { 获取;放; }

    公共无效BtnClick()
    {
        MsgBox.ShowInfo("Btn 单击",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>

ShellView模型:

公共接口 IShellViewModel :IScreen
{
    ProjectInfoViewModel ProjectInfo { 获取;放; }
    ProjectsViewModel 项目 { 获取;放; }
}

[导出(类型(IShellViewModel))]
公共类 ShellViewModel:Conductor.Collection.AllActive,
    IShellViewModel、IPartImportsSatisfiedNotification

{
    [进口]
    公共 ProjectInfoViewModel ProjectInfo { 获取;放; }

    [进口]
    公共 ProjectsViewModel 项目 { 获取;放; }

    公共无效OnImportsSatisfied()
    {
    }
}

如果我尝试编译此代码,则会出现错误:

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

StackTrace:

  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

Hi I user WPF/Calibur Micro/MEF. I I try have more active screen in shell. First I define screens - User controls.

  1. Screen - View model
public interface IProjectsViewModel
{

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

}

2.Screen -View Model

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

I second screen I have only one button if user click on the button it showed message box.

No most important part shell. Shell is WPF window.

View:

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

If I try compile this code I get error:

Could not locate any instances of contract
CaliburnSkelet.ViewModels.IShellViewModel.

StackTrace:

  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)

I tried make a test and change properties shell view model class.

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

I don’t import view models classes to shell view model with MEF but I create new instances in method OnImportsSatisfied.

Application run but if I click on button variable MsgBox is null.

Code from ProjectInfoViewModel class:

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

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

Where can be problem?

here? Conductor.Collection.AllActive

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2024-12-17 05:03:26

在您的 ShellViewModel 类型上,ProjectInfoProject 属性的类型应为 IProjectInfoViewModelIProjectsViewModel,因为这些是您导出的类型,您应该针对这些接口进行工作。

On your ShellViewModel type, the ProjectInfo and Project properties should be of type IProjectInfoViewModel and IProjectsViewModel, as these are the types you have exported, and you should be working against those interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文