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

4.1k views Asked by At

Everything is fine till I add a resource dictionary. Then it looks like:

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
{
...
}}

My error:

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?)   

My project name: MyApp Folder with ViewModels and Common also is caled: MyApp

What am I doing incorrectly while adding resource dictionary?

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

}}
2

There are 2 answers

0
Dragos Stoica On BEST ANSWER

Have you tried to change the order of the declarations ? Converters should be declared at first in ResourceDictionary.

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

        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
0
almulo On

My guess is that, somehow, adding the Converter directly to the MergedDictionary requires the class to implement IComponentConnector (and hence implement InitializeComponent()). Since Application doesn't implement IComponentConnector, you get this error.

Try moving the Converter definition around. Include it inside a ResourceDictionary or move it down to the Resources directly.

EDIT: Ok, seems like we all guessed the same at the same time :P