我希望在应用程序中有一个跨页面持续存在的菜单。实现这一目标的一种方法是在应用程序的每个页面中声明菜单布局和事件处理程序,但这将是糟糕的做法。
我试图实现一个页面,声明菜单和事件处理程序,然后有所有其他继承它,像下面的例子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");
}
}
}
在Silverlight中发明真正自定义的UI时要小心…微软对Silverlight应用程序的外观和感觉非常挑剔。他们显然对XNA应用更宽容。
要回答您的问题,我的方法是声明一个静态类,其中包含持久化菜单中的菜单项,然后为您的菜单创建一个自定义控件,该控件通过访问该静态数据来填充自己。然后,只需将这些控件中的一个添加到您希望它持久化的每个页面。
我有一种感觉,这将被微软退回,但你绝对可以给它一个机会。