gpt4 book ai didi

c# - WPF - 从 System.Windows.Application 继承

转载 作者:太空狗 更新时间:2023-10-29 21:56:05 26 4
gpt4 key购买 nike

我想拥有自己的继承自 System.Windows.Application 的应用程序类。问题是在 Application.xaml 文件中,我必须像这样声明应用程序:

    <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>

它在运行时运行良好,但是当我编辑项目属性时,Visual Studio 的项目属性编辑器收到以下错误消息:

An error occurred trying to load 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 element "Application" in the application definition file.

该项目是一个 VB.NET 项目。有人有解决方法吗?我想保留 application.xaml 文件。

最佳答案

我已经在 Visual Studio 2013 中尝试过,但没有遇到任何问题。所以,它应该是 VS 2008 特定的。您是否尝试过将您的应用程序类重新组织到一个单独的类库中?可能是锁定问题。

更新

为了实现它,我在 VB.NET VS2013 中执行的步骤:

  1. 创建自定义应用程序类。我在同一个程序集中创建了它。我的程序集名称是:WPFVBTest
Public Class CustomApplication
Inherits System.Windows.Application

End Class
  1. 修改 Application.xaml。添加命名空间引用并将根容器更改为自定义应用程序类。
  <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

通过这种方式,应用程序可以毫无问题地构建和运行。我希望它能帮助你解决你的错误。

关于c# - WPF - 从 System.Windows.Application 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/940644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com