如何在XAML中扩展WPF页面类以及后面的代码? [英] How to extend WPF Page class in XAML and code behind?

查看:105
本文介绍了如何在XAML中扩展WPF页面类以及后面的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个XAML页面",我想将它们的共同方面整合到我称为ACPage的Page类扩展中.尽管我认为我已经很接近了,但我必须缺少一些必不可少的知识.有人可以告诉我我想念什么吗?我正在寻找有关此(相当基本)主题的文章,但没有找到.

我收到以下警告:

警告1"Al_Cr.PageWelcome.InitializeComponent()"隐藏了继承的成员"Al_Cr.ACPage.InitializeComponent()".如果要隐藏,请使用new关键字. D:\ Projects \ Al Cr \ Al Cr \ Al Cr \ obj \ x86 \ Release \ PageWelcome.g.cs 50 21 Al Cr

这个错误:

错误2"Al_Cr.ACPage"不能是XAML文件的根目录,因为它是使用XAML定义的.第1行的位置15.D:\ Projects \ Al Cr \ Al Cr \ Al Cr \ PageWelcome.xaml 1 15 Al Cr

警告指向自动生成的PageWelcome.InitializeComponent()函数定义.错误指向下面的<big>local:ACPage</big>的末尾.

我的ACPage.xaml& ACPage.xaml.cs的定义如下:

I have several XAML "Page"s for which I would like to pull the facets that are in common together into a Page class extension I am calling ACPage. I must be missing some essential piece of knowledge, although I think I am pretty close. Can someone tell me what I am missing? I looked for an article on this (rather basic) topic, but didn''t find one.

I get the following warning:

Warning 1 ''Al_Cr.PageWelcome.InitializeComponent()'' hides inherited member ''Al_Cr.ACPage.InitializeComponent()''. Use the new keyword if hiding was intended. D:\Projects\Al Cr\Al Cr\Al Cr\obj\x86\Release\PageWelcome.g.cs 50 21 Al Cr

and this error:

Error 2 ''Al_Cr.ACPage'' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 15. D:\Projects\Al Cr\Al Cr\Al Cr\PageWelcome.xaml 1 15 Al Cr

The warning points to the auto-generated PageWelcome.InitializeComponent() function definition. The error points to the end of the <big>local:ACPage</big>, below.

My ACPage.xaml & ACPage.xaml.cs are defined thusly:

<Page x:Class="Al_Cr.ACPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      Style="{StaticResource StylePage}">
</Page>





public partial class ACPage : Page {
    public ACPage() {
    }
}



我的PageWelcome.xaml&因此,定义了ACPage的子类PageWelcome.xaml.cs:



My PageWelcome.xaml & PageWelcome.xaml.cs which is a subclass of ACPage are defined thusly:

<<big>local:ACPage</big> x:Class="Al_Cr.PageWelcome"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="clr-namespace:Al_Cr" >
    <Canvas>
         ...Welcome page stuff...
    </Canvas>
</local:ACPage>





public partial class PageWelcome : ACPage {
     public PageWelcome() {
         InitializeComponent();
     }
}



我正在使用MS Visual Studio C#2010 Express(版本10.0.30319.1)和.NET 4.0.30319.



I am using MS Visual Studio C# 2010 Express (Version 10.0.30319.1) and .NET 4.0.30319.

Thank you in advance!

推荐答案

我无法帮助您解决XAML错误,但警告可以解释.

在WPF页面中,InitializeComponent()是设计者生成的非虚拟方法(该方法有名称吗?).在.cs代码隐藏文件中,单击InitializeComponent()并单击F12以查看设计器生成的代码.

由于您的派生页面继承了另一个WPF页面对象,因此这两个对象都具有非虚拟的设计人员生成的方法,称为InitializeComponent(). C#认为对于基类和派生类而言,这都是不好的形式(:rolleyes:),它们都实现相同名称的非虚拟/重写方法,并向您发出警告.

但是,由于方法是从各自的构造函数中调用的,因此警告您可以忽略此警告,因为将从正确的构造函数中调用正确的InitializeComponent()方法.

我很想听听了解WPF的人对错误有何看法-以及是否/如何从另一个继承一个WPF页面.

克里斯
I can''t help you with the XAML error, but the warning I can explain.

In a WPF page, InitializeComponent() is a designer-generated, non-virtual (is there a name for that?) method. In the .cs code-behind file, click the InitializeComponent() and hit F12 to view the designer generated code.

Since your derived page inherits another WPF page object, both objects have non-virtual designer-generated methods called InitializeComponent(). C# considers it bad form ( :rolleyes: ) for base and derived classes both to implement non-virtual/override methods of the same name and gives you a warning about it.

However, since the methods are getting called from their respective constructors, it''s a warning that you can ignore because the right InitializeComponent() method will be called from the right constructor.

I''m interested to hear what people who know WPF have to say about the error - and if/how you can inherit one WPF page from another.

Chris


嗨!

您收到该错误,因为:当前不支持从另一个XAML生成的类派生XAML生成的类."
解决方案是:您需要在代码中全部定义基类,而无需使用XAML."
:((

来源

Ike
Hi!

You get that error, because: "Currently deriving a XAML generated class from another XAML generated class is not supported."
Solution is: "You need to define your base class all in code without using XAML."
:((

Source

Ike


可能值得在SO上查看相同的主题:

http://stackoverflow.com/questions/39843/how- do-i-create-a-base-page-in-wpf [
Probably worth checking out the same topic on SO:

http://stackoverflow.com/questions/39843/how-do-i-create-a-base-page-in-wpf[^]

Matt Hamilton''s response there suggesting the use of attached events is also worth considering.


这篇关于如何在XAML中扩展WPF页面类以及后面的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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