WPF Application base class?

I'm not even sure that this is possible, but I just started developing WPF in several new projects and tried to wrap some common functions by creating a mini-framework. Just things like exception handling and thread control.

What I would like to do is replace this line ...

public partial class App : Application

from

public partial class App : MyFrameworkApplication

I have libraries and links installed, but I get an error message about the "partially" declared class of the App , presumably because it still refers to the old base class.

Any ideas? Thanks.

EDIT: @Jeff M: No, your solution did not work. I suspect MyFrameworkApplication actually in the library, and the namespace declaration z does not recognize the library namespace. I got a link to it in App.xaml.cs, but a suspicious search error:

Error 3 Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'MyLibraryNamespace' that is not included in the assembly.

I can work around the problem by creating a proxy class in the local namespace and getting it from the library class ... but it's a little smelly.

+6
inheritance wpf
source share
1 answer

I suspect the main XAML root is still Application , not MyFrameworkApplication . I assume that the generated bumble uses the root as the parent class. Try changing it to the appropriate names.

eg.

 <z:MyFrameworkApplication x:Class="MyNamespace.App" ... xmlns:z="clr-namespace:MyNamespace"> ... </z:MyFrameworkApplication> 

My suspicions seem to be true.

From the docs in Code-Behind and XAML in WPF :

Code Handling, Event Handler, and Partial Class Requirements in WPF

  • A partial class must be obtained from a type that supports the root element. (highlight)
  • Note that when you perform the default compilation assembly steps, you can leave the derivation empty in the partial class definition on the code side. The compiled result will be the root page support type, which is the basis for the partial class, even if it is not specified. However, relying on this behavior is not the best practice.
  • The event handlers that you write in the code behind must be instance methods and cannot be static methods. These methods must be defined by a partial class in the CLR namespace, identified by x: class . You cannot qualify an event handler name to tell the XAML processor to search for an event handler to post events in another class.
  • The handler must match the delegate for the corresponding event in the support type system.
  • For the Microsoft Visual Basic language, you can specifically use the Handles keyword to bind handlers using instances and events in a handler declaration, rather than attaching handlers with attributes in XAML. However, this method has some limitations because the Handle keyword cannot support all the specific features of the WPF event system, such as specific routes to event scripts or related events. See Handling Visual Basic and WPF Events for more information.

The root type of the application in encoding and in xaml must match.

+11
source share

All Articles