C# 为什么我只能在设计时在WPF UserControl及其所有者窗口中获得生成错误?

C# 为什么我只能在设计时在WPF UserControl及其所有者窗口中获得生成错误?,c#,wpf,xaml,prism,prism-6,C#,Wpf,Xaml,Prism,Prism 6,我有一个main菜单用户控件,我在main窗口视图中使用该控件,它有一个main窗口视图模型主菜单标记以如下方式开始: <UserControl x:Class="ApptEase.Client.Shell.Controls.MainMenu" .... d:DesignHeight="25"> <UserControl.DataContext> <shell:MainWindowViewM

我有一个
main菜单
用户控件,我在
main窗口
视图中使用该控件,它有一个
main窗口视图模型
<代码>主菜单标记以如下方式开始:

<UserControl x:Class="ApptEase.Client.Shell.Controls.MainMenu"
             ....
             d:DesignHeight="25">
    <UserControl.DataContext>
        <shell:MainWindowViewModel />
    </UserControl.DataContext>
    <Menu Height="25" VerticalAlignment="Top">
        <MenuItem Header="_File">
My
MainWindowViewModel
源自
BaseViewModel

public abstract class BaseViewModel : BindableBase
{
    protected BaseViewModel()
    {
        AppState = ((App)Application.Current).AppState;
    }
    ...
}
如果我注释掉
((App)Application.Current).AppState
行,错误就会消失。然而,当我构建时,除了输出窗口中的“成功构建”之外,我看不到任何输出,并且应用程序启动良好,即
BaseViewModel
ctor执行良好,没有任何异常


如果我别无选择,只能接受无害的错误消息,有没有办法抑制它们?我看不到编译器指令在XAML标记中起作用。

我不知道这是一个“全面”解决方案还是一个正确的解决方案,但我通过将我的
BaseViewModel
ctor更改为如下所示,解决了我的问题:

protected BaseViewModel()
{
    if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    {
        _app = (App)Application.Current;
    }
}

似乎在设计模式下,
应用程序。当前的
不是
应用程序

类型。您能否提供完整的示例(可能作为存档)?我不熟悉prism,但这看起来像是App.xaml/App.xaml的问题。cs@cdmnk我基于Prism Unity项目模板创建了一个新项目,只添加了两个简单的内容,但基本应用程序仅使用Unity显示主窗口。如果进行生成,则可以看到错误。这里有一个指向我的Google drive的链接:@cdmnk我想它可能与设计时的
应用程序类似。当前的
不是
应用程序的类型,而是仅在运行时。
protected BaseViewModel()
{
    if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    {
        _app = (App)Application.Current;
    }
}