如何在应用程序的所有页面中实现类似标签的菜单,而无需反复编写相同的代码

时间:2011-07-19 08:29:13

标签: inheritance windows-phone-7 event-handling

我希望我的应用程序中的菜单可以跨页面存在。实现这一目标的一种方法是在应用程序的每个页面中声明菜单布局和事件处理程序,但这将是不好的做法。

我试图实现一个声明菜单和事件处理程序的页面,然后让其他所有继承它,就像下面http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/6c30d7e5-741f-4ce7-8306-72cae73d7ff4中的示例一样,但它似乎不适用于事件处理程序。有没有人有这方面的经验或知道其他更好的方法?

<InheritedPage:PhoneApplicationPageDerived 
    x:Class="InheritedPage.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:InheritedPage="clr-namespace:InheritedPage;assembly=InheritedPage"
    xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}">

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitleGrid is the name of the application and page title-->
        <Grid x:Name="TitleGrid" Grid.Row="0">
            <TextBlock Text="MY APPLICATION" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>
            <TextBlock Text="page title" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>
        </Grid>

        <!--ContentGrid is empty. Place new content here-->
        <Grid x:Name="ContentGrid" Grid.Row="1">
            <Button Content="Virtual" Height="70" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="160" Click="SomeVirtualMethod" />
            <Button Content="Base" Height="70" HorizontalAlignment="Left" Margin="320,0,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="SomeBaseMethod" />
        </Grid>
    </Grid>

</InheritedPage:PhoneApplicationPageDerived>





using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;

namespace InheritedPage {

    public class PhoneApplicationPageBase : PhoneApplicationPage {

        public PhoneApplicationPageBase() {
        }

        public virtual void SomeVirtualMethod(object sender, RoutedEventArgs e) {
            Debug.WriteLine("PhoneApplicationPageBase Virtual Method");
        }

    }

    public class PhoneApplicationPageDerived : PhoneApplicationPageBase {

        public PhoneApplicationPageDerived() {
        }

        public override void SomeVirtualMethod(object sender, RoutedEventArgs e) {
            Debug.WriteLine("PhoneApplicationPageDerived Override Method");
        }

    }

    public partial class MainPage : PhoneApplicationPageDerived {
        public MainPage() {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
        }


        // 1. COMMENT OUT THIS METHOD DECLARATION to see virtual inherited methods 
        // break Application.LoadComponent in the generated MainPage.g.cs

        public override void SomeVirtualMethod(object sender, RoutedEventArgs e) {
            Debug.WriteLine("MainPage Override Method");
        }


        // 2. MOVE THIS METHOD DECLARATION into PhoneApplicationPageBase to see non virtual inherited 
        // methods break Application.LoadComponent in the generated MainPage.g.cs

        public void SomeBaseMethod(object sender, RoutedEventArgs e) {
            Debug.WriteLine("PhoneApplicationPageBase Base Method");
        }


    }
}

1 个答案:

答案 0 :(得分:0)

在Silverlight中发明真正的自定义UI时要小心......微软对Silverlight应用程序的外观非常挑剔。他们显然对XNA应用程序更加宽容。

要回答您的问题,我的方法是声明一个包含持久菜单中菜单项的静态类,然后为您的菜单创建一个自定义控件,通过访问该静态数据来填充自己。然后,只需将其中一个控件添加到您希望其保留的每个页面。

我有一种感觉,这会被MS踢回来,但你绝对可以试一试。