WPF应用程序基类?

标签 wpf inheritance

我什至不确定这是否可能,但我刚刚开始在几个新项目上开发 WPF,并尝试通过创建一个迷你框架来包装一些通用功能。只是诸如异常处理和线程管理之类的事情。

我想做的是替换这条线...
public partial class App : Application

public partial class App : MyFrameworkApplication
我已经设置并引用了库,但是我收到关于“部分”声明的错误 App类,大概是因为它仍然引用旧的基类。

有任何想法吗?谢谢。

编辑:@Jeff M:不,您的解决方案不起作用。我怀疑是因为 MyFrameworkApplication实际上是在图书馆和z命名空间声明无法识别库的命名空间。我在 App.xaml.cs 中引用了它,但可疑的错误是:
Error 3 Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'MyLibraryNamespace' that is not included in the assembly.
我可以通过在本地命名空间中创建一个代理类并让它从库类派生来规避这个问题......但它有点臭。

最佳答案

我怀疑这是因为底层 XAML 根仍然是 Application而不是 MyFrameworkApplication .我猜生成的 baml 使用根作为它的父类。尝试将其更改为适当的名称。
例如。,

<z:MyFrameworkApplication x:Class="MyNamespace.App"
             ...
             xmlns:z="clr-namespace:MyNamespace">
    ...
</z:MyFrameworkApplication>
看来我的怀疑是对的。
来自 Code-Behind and XAML in WPF 中的文档:

Code-behind, Event Handler, and Partial Class Requirements in WPF

  • The partial class must derive from the type that backs the root element. (emphasis mine)
  • Note that under the default behavior of the markup compile build actions, you can leave the derivation blank in the partial class definition on the code-behind side. The compiled result will assume the page root's backing type to be the basis for the partial class, even if it not specified. However, relying on this behavior is not a best practice.
  • The event handlers you write in the code behind must be instance methods and cannot be static methods. These methods must be defined by the partial class within the CLR namespace identified by x:Class. You cannot qualify the name of an event handler to instruct a XAML processor to look for an event handler for event wiring in a different class scope.
  • The handler must match the delegate for the appropriate event in the backing type system.
  • For the Microsoft Visual Basic language specifically, you can use the language-specific Handles keyword to associate handlers with instances and events in the handler declaration, instead of attaching handlers with attributes in XAML. However, this technique does have some limitations because the Handles keyword cannot support all of the specific features of the WPF event system, such as certain routed event scenarios or attached events. For details, see Visual Basic and WPF Event Handling.

代码隐藏和 xaml 中的根应用程序类型必须一致。

关于WPF应用程序基类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3465549/

相关文章:

c# - 在 TextBlock 中将文本的一部分设为粗体

c# - 按钮启用 WPF

c# - PropertyGrid 和对象的动态类型

.net - WPF 中的置于前台

java - 带有类型列表和继承的方法

swift : How to use enum variable from super class?

继承表单时不显示 C# 设计器

c# - 绑定(bind)到 IEnumerable<object> 依赖属性

Javascript 调用并应用构造函数

c++ - 什么是对象切片?