另一个WPF项目中的WPF项目。命令不起作用

时间:2015-07-08 10:40:52

标签: wpf mvvm

我有一个包含2个WPF项目的项目解决方案。项目B包含在项目A中并被调用。

我遇到的问题是,项目B MainWindow上的按钮命令没有达到相应的ViewModel。当我将Project B作为一个独立的应用程序运行时,这种情况不会发生(一切都很好)。

在项目A和B中,我使用Galasofts MVVM光框架,在静态ViewModelLocator类中容纳我的视图模型。

任何想法都会非常感激。 感谢

编辑:为简单起见,我只包含了一个按钮。

ribbon:RibbonControl xmlns:Designer="clr-namespace:Nouvem.LabelDesigner.View.Designer"  x:Class="Nouvem.LabelDesigner.View.Designer.DesignerView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"           
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:command="http://www.galasoft.ch/mvvmlight"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:systemMessage="clr-namespace:Nouvem.LabelDesigner.View.SystemMessage"
        xmlns:loc="clr-namespace:Nouvem.Shared.Localisation;assembly=Nouvem.Shared"
        UseLayoutRounding="True"
        mc:Ignorable="d"
        d:DesignHeight="1000" d:DesignWidth="1200"
        Background="White"
        DataContext="{Binding LabelDesigner, Source={StaticResource Locator}}" >
    <Grid>
        <Button Command="{Binding TestCommand}"/>
    </Grid>

</ribbon:RibbonControl>

视图模型

 public RelayCommand TestCommand { get; set; }

 public LabelDesignerViewModel()
        {
            if (this.IsInDesignMode)
            {
                return;
            }    

            this.TestCommand = new RelayCommand(() =>
            {
                // test - not hitting when called from another project
            });
}
`

1 个答案:

答案 0 :(得分:0)

如果您在项目A中执行new ProjectB.MainWindow.Show()之类的操作,则会忽略Project B/App.xaml.cs中的所有逻辑和Project B/App.xaml中的资源。 如果您运行项目A,则Application.CurrentProjectA.App且ProjectB.App未实例化。

有两种解决方案:

<强> 1。将项目B创建为库:

创建名为Project B Library的新共享库,您将从项目A以及项目B中引用它。项目B将只是一个exe容器。

<强> 2。将Project B作为新流程启动

使用Process.Start()方法启动项目B.您可以使用netNamedPipeBinding与Project B交互WCF