MyApp.添加ResourceDictionary后,应用程序不包含Initialize的定义

本文关键字:包含 Initialize 定义 应用程序 添加 ResourceDictionary MyApp | 更新日期: 2023-09-27 17:59:48

在添加资源字典之前,一切都很好。然后它看起来像:

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>

应用程序.g.cs:

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

主菜单按钮可见性转换器:

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("");
    }
}}

MyApp.添加ResourceDictionary后,应用程序不包含Initialize的定义

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

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

我的猜测是,以某种方式,将Converter直接添加到MergedDictionary需要该类实现IComponentConnector(从而实现InitializeComponent())。由于Application没有实现IComponentConnector,因此会出现此错误。

尝试移动转换器定义。将其包含在ResourceDictionary中,或者直接将其移到Resources中。

编辑:好吧,似乎我们都在同一时间猜到了同样的东西:p