0

我有一个MainMenu在我的MainWindow视图中使用的用户控件,它有一个MainWindowViewModel. MainMenu标记开始像:

<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">

在设计模式下,我的错误列表中有 2 个错误,它们都是:

无法将“System.Windows.Application”类型的对象转换为“ApptEase.Client.App”类型

第一个出现在<shell:MainWindowViewModel />上面用户控件标记的行中。第二个出现在下面<shell:MainMenu Grid.Row="0" />MainWindow标记摘录行中。

主窗口摘录:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <shell:MainMenu Grid.Row="0" />
    <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />
</Grid>

MainWindowViewModel来自BaseViewModel

public abstract class BaseViewModel : BindableBase
{
    protected BaseViewModel()
    {
        AppState = ((App)Application.Current).AppState;
    }
    ...
}

如果我注释掉该((App)Application.Current).AppState行,错误就会消失。然而,当我构建时,除了“成功构建”之外,我在输出窗口中看不到任何输出,并且应用程序启动良好,即BaseViewModelctor 执行良好,无一例外。

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

4

1 回答 1

0

我不知道这只是一个“包罗万象”还是一个正确的解决方案,但我已经通过将我的BaseViewModelctor 更改为如下所示解决了我的问题:

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

似乎在设计模式下,Application.Current不是 type App

于 2017-01-15T06:37:52.623 回答