MyApp.App 添加 ResourceDictionary 后不包含 Initialize 的定义

MyApp.App does not contain a definition for Initialize after adding ResourceDictionary

一切正常,直到我添加资源字典。然后它看起来像:

App.xaml:

<Application x:Class="MyApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
         xmlns:local="clr-namespace:MyApp.MyApp.Common">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

        </ResourceDictionary.MergedDictionaries>
        <local:MainMenuButtonVisibilityConverter x:Key="MainMenuButtonVisibilityConverter" />  <!-- withoot this line is fine-->
    </ResourceDictionary>
</Application.Resources>

App.g.cs:

namespace MyApp {
...
    public static void Main() {
        MyApp.App app = new MyApp.App();
        app.InitializeComponent(); // <------ here is the problem
        app.Run();
    }
}

MainMenuButtonVisibilityConverter:

namespace MyApp.MyApp.Common
{
public class MainMenuButtonVisibilityConverter : IValueConverter
{
...
}}

我的错误:

Error   2   'MyApp.MyApp.App' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'MyApp.MyApp.App' could be found (are you missing a using directive or an assembly reference?)   

我的项目名称:MyApp 包含 ViewModels 和 Common 的文件夹也被命名为:MyApp

我在添加资源字典时做错了什么?

App.xaml.cs

namespace MyApp.MyApp
{
public partial class App : Application
{
    private Shell shell;
    private MyAppMain myAppMain;

    public App()
    {
    }

    private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.Message, "", MessageBoxButton.OK, MessageBoxImage.Hand);
        e.Handled = true;
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        base.Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(this.Dispatcher_UnhandledException);
        myAppMain = new MyAppMain("");
    }

}}

您是否尝试过更改声明的顺序?转换器应首先在 ResourceDictionary 中声明。

 <ResourceDictionary>
 <local:MainMenuButtonVisibilityConverter x:Key="MainMenuButtonVisibilityConverter" />  <!-- withoot this line is fine-->
        <ResourceDictionary.MergedDictionaries>

        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>

我的猜测是,不知何故,将转换器直接添加到 MergedDictionary 需要 class 来实现 IComponentConnector(并因此实现 InitializeComponent())。由于 Application 未实现 IComponentConnector,您会收到此错误。

尝试移动 Converter 定义。将其包含在 ResourceDictionary 中或直接将其向下移动到 Resources。

编辑:好吧,好像我们都同时猜到了一样:P