如何在WPF中创建基本页面? [英] How do I create a base page in WPF?
问题描述
我决定所有WPF页面都需要注册一个路由事件。而不是包含
公共静态只读RoutedEvent MyEvent = EventManager.RegisterRoutedEvent( MyEvent,RoutingStrategy.Bubble,typeof(RoutedEventHandler), typeof(BasePage));
在每个页面上,我决定创建一个基础页面(名为BasePage)。我将上面的代码行放在我的基本页面中,然后更改了其他一些页面以从BasePage派生。我无法克服此错误:
错误12'CTS.iDocV7.BasePage'不能
是a的根XAML文件,因为它
是使用XAML定义的。第1行
位置
22. C:\Work\iDoc7\CTS.iDocV7\UI\Quality\\QualityControlQueuePage.xaml 1 22 CTS.iDocV7
当我可以在任何wpf页面中放置想要使用的事件,属性,方法等时,有人知道如何最好地创建基本页面吗? / p>
在我当前的项目中,这是我的操作方式。
首先,我定义了一个类(如@Daren Thomas所说-只是一个普通的旧C#类,没有关联的XAML文件),就像这样(是的,这是一个真实的类-最好不要问):
公共类PigFinderPage:页面
{
/ *在此处添加自定义事件和属性* /
}
然后我创建一个新页面并将其XAML声明更改为:
< my:PigFinderPage x:Class = Qaf.PigFM.WindowsClient.PenSearchPage
xmlns = http://schemas.microsoft.com / winfx / 2006 / xaml / presentation
xmln s:x = http://schemas.microsoft.com/winfx/2006/xaml
xmlns:my = clr-namespace:Qaf.PigFM.WindowsClient
/>
因此,我在我的命名空间中将其声明为PigFinderPage。必须使用类似的语法声明您需要的所有页面范围资源:
< my:PigFinderPage.Resources>
<!-您的资源在这里->
< / my:PigFinderPage.Resources>
最后,切换到此新页面的代码隐藏,并更改其类声明,以便它
公共局部类EarmarkSearchPage:PigFinderPage
code>
记住要保留它作为局部类。
为我服务-我可以在 PigFinderPage中定义一堆自定义属性和事件,并在所有后代中使用它们。
I have decided that all my WPF pages need to register a routed event. Rather than include
public static readonly RoutedEvent MyEvent= EventManager.RegisterRoutedEvent("MyEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BasePage));
on every page, I decided to create a base page (named BasePage). I put the above line of code in my base page and then changed a few of my other pages to derive from BasePage. I can't get past this error:
Error 12 'CTS.iDocV7.BasePage' cannot
be the root of a XAML file because it
was defined using XAML. Line 1
Position
22. C:\Work\iDoc7\CTS.iDocV7\UI\Quality\QualityControlQueuePage.xaml 1 22 CTS.iDocV7
Does anyone know how to best create a base page when I can put events, properties, methods, etc that I want to be able to use from any wpf page?
解决方案 Here's how I've done this in my current project.
First I've defined a class (as @Daren Thomas said - just a plain old C# class, no associated XAML file), like this (and yes, this is a real class - best not to ask):
public class PigFinderPage : Page
{
/* add custom events and properties here */
}
Then I create a new Page and change its XAML declaration to this:
<my:PigFinderPage x:Class="Qaf.PigFM.WindowsClient.PenSearchPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Qaf.PigFM.WindowsClient"
/>
So I declare it as a PigFinderPage in the "my" namespace. Any page-wide resources you need have to be declared using a similar syntax:
<my:PigFinderPage.Resources>
<!-- your resources go here -->
</my:PigFinderPage.Resources>
Lastly, switch to the code-behind for this new page, and change its class declaration so that it derives from your custom class rather than directly from Page, like this:
public partial class EarmarkSearchPage : PigFinderPage
Remember to keep it as a partial class.
That's working a treat for me - I can define a bunch of custom properties and events back in "PigFinderPage" and use them in all the descendants.
这篇关于如何在WPF中创建基本页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!