WPF - inherits 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 application as follows:

    <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 fine at runtime, but when I edit the project properties, I get the following error message using the Visual Studio project properties editor:

An error occurred while loading 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 Application element in the application definition file.

The project is a VB.NET project. Does anyone have a workaround? I want to save the application.xaml file.

thanks

+5
source share
4 answers

I tried this with Visual Studio 2013, but I had no problems. Thus, it should comply with VS 2008. Have you tried to reorganize your application class into a separate class library? Perhaps this is a blocking problem.

UPDATE

The steps I took in VB.NET VS2013 to make this happen:

  • Create your own application class. I created it in the same assembly. My build name: WPFVBTest
Public Class CustomApplication
  Inherits System.Windows.Application

End Class
  1. Modify Application.xaml. Add a namespace reference and change the root container to your own 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. : Application.xaml.vb CustomApplication
Class Application
  Inherits CustomApplication

End Class
  1. . , , MyWpfExtension.vb . , . , . :
Partial Class Application
  Inherits CustomApplication

, . , .

+2

, , ... Visual Studio 2015? !

, VS 2017 ; .

+2

If he says <src: MyBaseApplication.Resources>?

-1
source

Applications usually point to a window and try to get this information from the xaml file; but you have a link 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 this error ...

My suggestion is to override the window or use extensions in the application, for example:

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

Use it as follows:

Application.Current.GetAwesomeName();
-1
source

All Articles