我正在尝试使用NavigationView
创建一个UWP应用。我的问题是找到Header
更改当前显示页面的方法。我的应用程序基于Microsoft UWP示例“AppUIBasics”。在示例中,Header
的{{1}}始终显示为“欢迎”,因为其硬编码但我希望它根据所选页面进行更改。
MainPage.xaml中:
NavigationView
MainPage.xaml.cs中:
<NavigationView x:Name="NavView"
ItemInvoked="NavView_ItemInvoked"
SelectionChanged="NavView_SelectionChanged"
Loaded="NavView_Loaded"
Canvas.ZIndex="0">
<NavigationView.HeaderTemplate>
<DataTemplate>
<Grid Margin="24,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="appTitle" Style="{StaticResource TitleTextBlockStyle}"
FontSize="28"
VerticalAlignment="Center"
Text="Welcome"/>
<CommandBar Grid.Column="1"
HorizontalAlignment="Right"
VerticalAlignment="Top"
DefaultLabelPosition="Right"
Background="{ThemeResource SystemControlBackgroundAltHighBrush}">
<AppBarButton Label="Refresh" Icon="Refresh"/>
<AppBarButton Label="Import" Icon="Import"/>
</CommandBar>
</Grid>
</DataTemplate>
</NavigationView.HeaderTemplate>
<NavigationView.MenuItems>
<NavigationViewItem x:Uid="HomeNavItem" Content="" Tag="home">
<NavigationViewItem.Icon>
<FontIcon Glyph=""/>
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItemSeparator/>
<NavigationViewItem x:Uid="ConnectionNavItem" Icon="world" Content="" Tag="connection"/>
<NavigationViewItem x:Uid="CameraNavItem" Icon="video" Content="" Tag="camera"/>
</NavigationView.MenuItems>
<Frame x:Name="rootFrame" Margin="24">
<Frame.ContentTransitions>
<TransitionCollection>
<NavigationThemeTransition/>
</TransitionCollection>
</Frame.ContentTransitions>
</Frame>
</NavigationView>
答案 0 :(得分:5)
首先你需要&#34; un-hardcode&#34;模板中的标题文字,而不是您可以使用{Binding}
的文字,它将提供分配给NavigationView
Header
属性的值:
<NavigationView.HeaderTemplate>
<DataTemplate>
<Grid Margin="24,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="appTitle" Style="{StaticResource TitleTextBlockStyle}"
FontSize="28"
VerticalAlignment="Center"
Text="{Binding}"/>
<CommandBar Grid.Column="1"
HorizontalAlignment="Right"
VerticalAlignment="Top"
DefaultLabelPosition="Right"
Background="{ThemeResource SystemControlBackgroundAltHighBrush}">
<AppBarButton Label="Refresh" Icon="Refresh"/>
<AppBarButton Label="Import" Icon="Import"/>
</CommandBar>
</Grid>
</DataTemplate>
</NavigationView.HeaderTemplate>
现在有两个地方可以设置标题内容。首先是SelectionChanged
事件:
private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
if (args.IsSettingsSelected)
{
rootFrame.Navigate(typeof(SettingsPage));
NavView.Header = "Settings";
}
else
{
NavigationViewItem item = args.SelectedItem as NavigationViewItem;
NavView_Navigate(item);
//just example, maybe you want to Content or something else
NavView.Header = item.Tag.ToString();
}
}
您还可以通过添加设置标题的公共方法从任何地方设置Header
:
public void SetHeader(string header)
{
NavView.Header = header;
}
然后在您认为合适的任何地方使用MainPage.Current.SetHeader( something )
。
然而,最好的解决方案是创建一个基类,其他页面将派生自:
public class BasePage : Page
{
public virtual string Header => "";
}
然后每个具体的页面都可以覆盖此属性:
public sealed partial class HomePage : BasePage
{
public override string Header => "Home";
public HomePage()
{
this.InitializeComponent();
}
}
注意 - 请记住,您还必须在XAML中更新页面的基本类型:
<local:BasePage xmlns:local="using:AppNamespace"
x:Class="AppNamespace.HomePage" ...>
</local:BasePage>
然后在MainPage
我们使用数据绑定将Header
属性绑定到NavigationView
&#39; s Header
:
<NavigationView x:Name="NavView"
Header="{Binding Path=Content.Header, ElementName=rootFrame}" ...>
答案 1 :(得分:1)
定义界面
interface ISubPage
{
string NavTitile { get; }
}
在子页面中实现此接口
public sealed partial class HomePage : Page, ISubPage
{
public HomePage()
{
InitializeComponent();
}
public string NavTitile => "ArticlePage";
}
<NavigationView x:Name="NavView" Header="{Binding Path=Content.NavTitile, ElementName=ContentFrame}">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Home" Tag="Home" Content="Home"/>
</NavigationView.MenuItems>
<Frame x:Name="ContentFrame" Margin="24"/>
</NavigationView>