I have decided that all my WPF pages need to register a routed event. Rather than include
我已经决定我所有的WPF页面都需要注册一个路由事件。而不是包括
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:
在每个页面上,我决定创建一个基本页面(名为BasePage)。我将上面一行代码放在我的基本页面中,然后更改了一些其他页面以派生自BasePage。我无法克服这个错误:
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
12“CTS.iDocV7错误。BasePage不能是XAML文件的根,因为它是用XAML定义的。1号线位置22。C:\ \工作iDoc7 \ CTS.iDocV7 \ UI \ \ 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?
当我可以在任何wpf页面上放置我想要使用的事件、属性、方法等时,有人知道如何最好地创建基页吗?
5 个解决方案
#1
25
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):
首先,我定义了一个类(@Daren Thomas说——只是一个普通的老c#类,没有关联的XAML文件),像这样(是的,这是一个真正的类——最好不要问):
public class PigFinderPage : Page
{
/* add custom events and properties here */
}
Then I create a new Page and change its XAML declaration to this:
然后我创建一个新页面并将其XAML声明更改为以下内容:
<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。您需要使用类似的语法声明任何页面范围的资源:
<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.
这对我来说是一种享受——我可以在“PigFinderPage”中定义一组自定义属性和事件,并在所有的后代中使用它们。
#2
3
Also, have a look at Attached Events and see if you can attach your event to every Page in your app. Might be easier than a custom intermediary class.
另外,看看附件中的事件,看看是否可以将事件附加到应用程序的每个页面上。
#3
2
I'm not sure on this one, but looking at your error, I would try to define the base class with just c# (.cs) code - do not create one with XAML, just a standard .cs file that extends the WPF Page class.
我不太确定,但是看看您的错误,我将尝试使用c# (.cs)代码定义基类——不要使用XAML创建基类,而是使用扩展WPF页面类的标准.cs文件。
#5
1
Little update : I just tried to do it, and it didn't work. He is what I changed to solve the problem:
小更新:我只是试着去做,但是没有成功。他就是我为了解决问题而改变的人:
1.In many forums, you will read that the sub pages must inherit from a simple cs class, without XAML. Though it works. I do inherit from a normal XAML page without any problem.
1。在许多论坛中,您将看到子页面必须继承一个简单的cs类,而不包含XAML。虽然它的工作原理。我确实继承了一个正常的XAML页面,没有任何问题。
2.I replaced the following code :
2。我替换了以下代码:
<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"
/>
with
与
<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="using:Qaf.PigFM.WindowsClient"
/>
because when I had "clr-namespace" instead of "using", the Intellisense could recognize PigFinderPage, but not the compiler.
因为当我使用“clr-namespace”而不是“using”时,智能感知可以识别PigFinderPage,但不能识别编译器。
#1
25
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):
首先,我定义了一个类(@Daren Thomas说——只是一个普通的老c#类,没有关联的XAML文件),像这样(是的,这是一个真正的类——最好不要问):
public class PigFinderPage : Page
{
/* add custom events and properties here */
}
Then I create a new Page and change its XAML declaration to this:
然后我创建一个新页面并将其XAML声明更改为以下内容:
<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。您需要使用类似的语法声明任何页面范围的资源:
<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.
这对我来说是一种享受——我可以在“PigFinderPage”中定义一组自定义属性和事件,并在所有的后代中使用它们。
#2
3
Also, have a look at Attached Events and see if you can attach your event to every Page in your app. Might be easier than a custom intermediary class.
另外,看看附件中的事件,看看是否可以将事件附加到应用程序的每个页面上。
#3
2
I'm not sure on this one, but looking at your error, I would try to define the base class with just c# (.cs) code - do not create one with XAML, just a standard .cs file that extends the WPF Page class.
我不太确定,但是看看您的错误,我将尝试使用c# (.cs)代码定义基类——不要使用XAML创建基类,而是使用扩展WPF页面类的标准.cs文件。
#4
#5
1
Little update : I just tried to do it, and it didn't work. He is what I changed to solve the problem:
小更新:我只是试着去做,但是没有成功。他就是我为了解决问题而改变的人:
1.In many forums, you will read that the sub pages must inherit from a simple cs class, without XAML. Though it works. I do inherit from a normal XAML page without any problem.
1。在许多论坛中,您将看到子页面必须继承一个简单的cs类,而不包含XAML。虽然它的工作原理。我确实继承了一个正常的XAML页面,没有任何问题。
2.I replaced the following code :
2。我替换了以下代码:
<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"
/>
with
与
<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="using:Qaf.PigFM.WindowsClient"
/>
because when I had "clr-namespace" instead of "using", the Intellisense could recognize PigFinderPage, but not the compiler.
因为当我使用“clr-namespace”而不是“using”时,智能感知可以识别PigFinderPage,但不能识别编译器。