【问题标题】:WPF project within another WPF project. Commands not working另一个 WPF 项目中的 WPF 项目。命令不起作用
【发布时间】:2015-07-08 10:40:52
【问题描述】:

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

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

在项目 A 和 B 中,我都使用 Galasofts MVVM 灯光框架,将我的视图模型存放在静态 ViewModelLocator 类中。

任何想法将不胜感激。 谢谢

编辑:为简单起见,我只添加了 1 个按钮。

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

【问题讨论】:

  • 您能否提供处理这些命令的代码的简单示例?请同时包含 XAML 和 C#。

标签: wpf mvvm


【解决方案1】:

如果您在项目 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。启动项目 B 作为新进程

使用Process.Start()方法启动Project B。您可以使用netNamedPipeBinding与Project B通过WCF进行交互

【讨论】:

    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    相关资源
    最近更新 更多