WPF-从System.Windows.Application继承 [英] WPF - inherit from System.Windows.Application

查看:53
本文介绍了WPF-从System.Windows.Application继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to have my own application class that inherits from System.Windows.Application. The problem is that in the Application.xaml file, I have to declare the app like this :

    <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的项目属性编辑器收到以下错误消息:

It works great at runtime, but when I edit the project properties, I receive the following error message by the project properties editor of Visual Studio :

尝试为此项目加载应用程序定义文件时发生错误.无法解析文件"[...] \ Application.xaml".请在XAML编辑器中编辑文件以修复错误.在应用程序定义文件中找不到预期的根元素"Application".

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文件.

The project is a VB.NET project. Somebody have a workaround? I want to keep the application.xaml file.

谢谢

推荐答案

我已经在Visual Studio 2013中对其进行了尝试,但没有遇到任何问题.因此,它应特定于VS 2008.您是否尝试将应用程序类重新组织到一个单独的类库中?也许这是一个锁定问题.

I've tried it with Visual Studio 2013 but I did not experience any problem. So, it should be VS 2008 specific. Did you try to reorganize your application class into a separate class library? Maybe it is a locking issue.

更新

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

The steps I did in VB.NET VS2013 in order to make it happen:

  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

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

This way the application builds and runs without a problem. I hope it helps you to solve your error.

这篇关于WPF-从System.Windows.Application继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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