Karol Deland
Karol Deland

Reputation:

WPF - inherit from System.Windows.Application

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.

Upvotes: 7

Views: 2668

Answers (4)

Daniel Leiszen
Daniel Leiszen

Reputation: 1897

I've tried it with Visual Studio 2013 but I did not experience any problem. So, it should be VS 2008 specific. Did you try to reorganize your application class into a separate class library? Maybe it is a locking issue.

UPDATE

The steps I did in VB.NET VS2013 in order to make it happen:

  1. Create the custom application class. I created it in the same assembly. My assembly name was: WPFVBTest
Public Class CustomApplication
  Inherits System.Windows.Application

End Class
  1. Modify the Application.xaml. Add the namespace reference and change the root container to the custom application class.
  <self:CustomApplication x:Class="Application"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:self="clr-namespace:WPFVBTest"
        StartupUri="MainWindow.xaml">
    </self:CustomApplication>
  1. Modify the code behind file: Application.xaml.vb to inherit from the CustomApplication class
Class Application
  Inherits CustomApplication

End Class
  1. Build the application. One error should occur indicating that MyWpfExtension.vb is not in sync with the application class name. This is a generated class, but not regenerated at every build. I know its cheating, but this was the only way I could manage it. So I modified it manually:
Partial Class Application
  Inherits CustomApplication

This way the application builds and runs without a problem. I hope it helps you to solve your error.

Upvotes: 2

Logan
Logan

Reputation: 816

I know it's not going to be a popular answer, but... why don't you just upgrade to Visual Studio 2015? Its free!

Hell, they announced VS 2017 just yesterday; also free.

Upvotes: 1

Arcturus
Arcturus

Reputation: 27055

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

Upvotes: -1

Drew Hoskins
Drew Hoskins

Reputation: 4186

Should it say < src:MyBaseApplication.Resources > ?

Upvotes: -1

Related Questions