WPF:我可以继承 UserControl 但不提供 XAML 吗? [英] WPF: can I inherit a UserControl but provide no XAML?

查看:20
本文介绍了WPF:我可以继承 UserControl 但不提供 XAML 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展一个 3rd 方应用程序,它使用看起来像是 PRISM 的早期版本来提供可组合性.

我制作了一个视图的原型,它非常简单.为了将我的 UI 插入到第 3 方 UI 中,我的 UserControl 必须实现一个特定的界面(如 IThirdPartyView).

这很好.但是...现在我想以更生产的方式实现代码.一个关键点是我编写的 UserControls 需要不受污染,所以不要直接依赖 3rd Party 的东西.如果我们切换到另一个 3rd 方,我们不希望在代码中产生对另一个方的依赖.

作为面向对象的背景,我认为解决方案很简单:将我的 UI 放在一个独立的程序集中,不依赖于任何 3rd 方.我们称之为 CleanUserControl.然后在第 3 方特定实现中,扩展类 CleanUserControl,并让该类实现第 3 方特定接口并扩展我的 CleanUserControl 类,因此类声明类似于:

public class SpecificUserControl : CleanUserControl, IThirdPartyView{//IThirdPartyView 的实现}

但是……在实践中我很难做到这一点.

我设法让它编译,但在运行时点击了这个:

<前>组件CustomerProfile.ThirdParty.View.SecurityView2"没有由 URI '/CustomerProfile;component/view/securityview.xaml' 标识的资源

实际上,我想我的扩展程序只实现背后的代码并从父级继承 xaml,但我不知道这是否可以做到.

我希望我的问题很清楚,我无法解释!

解决方案

您不能继承 XAML.这是明确禁止的.最好的机会是为内容创建一个通用 XAML 模板并将其应用到派生"控件.您可以将模板放置在您想要的任何位置,无论是相同的程序集还是其他.

另一种可能性是实现您的父控件:

  • 将您的父控件创建为常规 XAML 控件
  • 编译程序集
  • 转到 obj 文件夹
  • 检查扩展名为 .g.cs 的控件的源代码
  • 复制自动生成的代码并重新开始以使用纯 C# 创建控件

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!

解决方案

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#

这篇关于WPF:我可以继承 UserControl 但不提供 XAML 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆