Paul
Paul

Reputation: 855

WPF: can I inherit a UserControl but provide no XAML?

I am extending a 3rd party application which uses what looks like an early incarnation of PRISM to provide composability.

I prototyped one view and it was pretty straightforward. In order to get my UI plugged into the 3rd party UI my UserControl had to implement a specific interface (like IThirdPartyView).

This was fine. BUT...now I want to implement the code in a more production manner. And one key point is that the UserControls I write need to be unpolluted, so not be directly dependant on 3rd Party stuff. IF we switch to another 3rd party we don't want to have dependencies on the other one baked into the code.

Being of an OO background I thought the solution was simple: have my UI in a seperate assembly which wasn't dependent on any 3rd party. Let's call it CleanUserControl. And then in a 3rd party specific implementation, extend the class CleanUserControl, and have that extended one implement the 3rd party specific interface as well as extend my CleanUserControl class, so a class declaration something like:

public class SpecificUserControl : CleanUserControl, IThirdPartyView
{
    // Implementation of IThirdPartyView
}

BUT...in practice I'm having trouble achieving this.

I managed to get it to compile but then hit this at runtime:

    The component 'CustomerProfile.ThirdParty.View.SecurityView2' does not have a
    resource identified by the URI '/CustomerProfile;component/view/securityview.xaml'

In effect I think I want my extension to ONLY implement the code behind and inherit the xaml from the parent, but I don't know if this can be done.

I hope my question is clear, I had trouble explaining it!

Upvotes: 2

Views: 1361

Answers (1)

user694833
user694833

Reputation:

You cannot inherit XAML. This is explicitly forbidden. Your best chance is to create a common XAML template for the content and apply it to your "derived" controls. You can place the template wherever you want, either same assembly or other.

Another possibility is to implement your parent control:

  • Create your parent control as a regular XAML control
  • Compile the assembly
  • Go to the obj folder
  • Check for the source code for your control with extension .g.cs
  • Copy the auto-generated code and start again to create your control with pure C#

Upvotes: 2

Related Questions