简体   繁体   English

自定义页面类与自动卸载事件

[英]Custom Page Class w/ automated unload Event

we have a rather large Silverlight application and we need to add some extra functionality to it. 我们有一个相当大的Silverlight应用程序,我们需要为它添加一些额外的功能。 The App consists of an Frame-Element and a TreeView w/ HyperlinkButtons for the navigation. 该应用程序包含一个Frame-Element和一个TreeView w / HyperlinkBut​​tons用于导航。 Every content which will be loaded into the main Frame is a Page. 将加载到主框架中的每个内容都是一个页面。

Now, I need to hook into every Page's unload event. 现在,我需要挂钩每个Page的unload事件。 Currently we use something like this: 目前我们使用这样的东西:

/* PageX.xaml */

<navigation:Page
    x:Class="Foo.Views.PageX" 
    <!-- namespacing -->
    Title="Test Page X"
    Unloaded="Page_Unloaded">

    ...

</navigation:Page>

Code-behind: 代码隐藏:

/* PageX.xaml.cs */

/* usings */

namespace Foo.Views
{
    public partial class PageX : Page
    {
        public PageX() {
            InitializeComponent();
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e) {
            /* CODE */
        }
    }
}

This approach need to be implemented on each and every Page, as the code within the unloaded method stays exactly the same... As I mentioned earlier, we have a couple of Pages and it would be much more useful to create a custom Page-class where this Page_Unloaded() is implemented directly, so that we don't need to alter every Page. 这种方法需要在每个页面上实现,因为卸载方法中的代码保持完全相同......正如我之前提到的,我们有几个页面,创建自定义页面会更有用。这个Page_Unloaded()直接实现的类,这样我们就不需要改变每一个Page。

Can you please tell me how to create such a custom Page-class? 你能告诉我如何创建这样的自定义页面类吗?

Thanks in advance for any help!! 在此先感谢您的帮助!!

Kind regards! 亲切的问候!

You could create a base Page class that all your pages inherit from which registers the event in the constructor... 您可以创建一个基页面类,您的所有页面都继承自构造函数中的事件...

public class BasePage : Page
{
    public BasePage()
    {
        Unloaded += Page_Unloaded;
    }

    void Page_Unloaded(object sender, RoutedEventArgs e)
    {

    }
}

Then all your pages could inherit from that... 然后你的所有页面都可以继承......

public partial class Page1 : BasePage
{
    public Page1()
    {
        InitializeComponent();
    }
}

...and in the xaml of each page... ......并在每页的xaml中......

<base:BasePage x:Class="WPFApp.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:base="clr-namespace:WPFApp">

    <Grid>

    </Grid>
</base:BasePage>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM