views:

168

answers:

2

I would like to have my own application class that inherits from System.Windows.Application. The problem is that in the Application.xaml file, I have to declare the app like this :

    <src:MyBaseApplication x:Class="MyApplication"
    xmlns:src="clr-namespace:MyApplication;assembly=WpfTestApplication"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="FPrincipal.xaml">    
    <Application.Resources>

    </Application.Resources>
</src:MyBaseApplication>

It works great at runtime, but when I edit the project properties, i receive the following error message by the project properties editor of Visual Studio :

An error occurred trying to load the application definition file for this project. The file '[...]\Application.xaml' could not be parsed. Please edit the file in the XAML editor to fix the error.

Could not find the expected root element "Application" in the application definition file.

The project is a VB.NET project. Somebody have a workaround? I want to keep the application.xaml file.

Thanks

A: 

Should it say < src:MyBaseApplication.Resources > ?

Drew Hoskins
Well you could, but since you are inheriting from Application, you can also use Application.Resources...
Arcturus
A: 

Applications usually points to a Window and tries to get this information from the xaml file; but you have a reference in your xaml file that Visual Studio cannot determine..

xmlns:src="clr-namespace:MyApplication;assembly=WpfTestApplication"

So VS cannot parse your xaml file, and thus gives that error...

My suggestion: override the Window, or use extensions on Application, like so :

public static class ApplicationExtensions
{
    public static string GetAwesomeName(this Application)
    {
        return "I am an awesome application";
    }
}

Use it like this:

Application.Current.GetAwesomeName();
Arcturus
You miss something. The error message specify that the Application tag is missing. It's true because the root tag is src:MyBaseApplication. VisualStudio should be wise enough to find the root tag with a different name. I already know that I can subclass the application in C#, but doing this we'll lose the .xaml file associated with the application.
Karol Deland
Have you considered slicing up those pieces of xaml into a ResourceDictionary and include that in the Resources of your Application?
Arcturus