为什么我的SplitView的内容和窗格在同一区域?

时间:2016-11-18 20:19:26

标签: c# xaml uwp uwp-xaml

我一整天都在寻找和寻找例子,我无法找到解决问题的方法。我在它的Pane中有一个带有汉堡包菜单的SplitView,我正在为所选的每个ListBoxItem加载一个框架。但是当我的页面加载时,它看起来像这样(橙色区域是页面背景): Start page 我也想知道如何设置我的任何帧在启动时加载,我试图将TodayListBoxItem设置为IsSelected =“true”并且IsEnabled =“true”但是我得到的是与上面显示的相同的启动页面,但是选择TodayListBoxItem。 这是我的XAML:

<Page
x:Class="Hamburger.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Hamburger"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <RelativePanel>
        <Button Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" FontSize="36" Click="HamburgerButton_Click" />
        <TextBlock Name="PageTitle" FontSize="30" Margin="70,0,0,0"></TextBlock>
    </RelativePanel>
    <SplitView Name="MySplitView" 
               Grid.Row="1" 
               DisplayMode="CompactInline" 
               OpenPaneLength="200" 
               CompactPaneLength="56" 
               HorizontalAlignment="Left">
        <SplitView.Pane>
            <ListBox SelectionMode="Single" 
                     Name="IconsListBox"
                     SelectionChanged="IconsListBox_SelectionChanged">
                <ListBoxItem Name="TodayListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE909;" />
                        <TextBlock Text="Today" FontSize="24" Margin="20,0,0,0" />
                    </StackPanel>
                </ListBoxItem>

                <ListBoxItem Name="ForecastListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE8F5;" />
                        <TextBlock Text="Forecast" FontSize="24" Margin="20,0,0,0" />
                    </StackPanel>
                </ListBoxItem>

                <ListBoxItem Name="SettingsListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE713;" />
                        <TextBlock Text="Settings" FontSize="24" Margin="20,0,0,0" />
                    </StackPanel>
                </ListBoxItem>

            </ListBox>
        </SplitView.Pane>
        <SplitView.Content >
            <Frame Name="ContentFrame" Background="Tomato"  HorizontalAlignment="Stretch" />
        </SplitView.Content>
    </SplitView>
</Grid>
</Page>

这是我的xaml.cs:

namespace Hamburger
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }
    private void HamburgerButton_Click(object sender, RoutedEventArgs e)
    {
        MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;
    }

    private void IconsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (TodayListBoxItem.IsSelected)
        {
            PageTitle.Text = "Today";
            ContentFrame.Navigate(typeof(TodayPage));
/*
            Trying to use a method in TodayPage so that it shows the current
            weather
*/
        }
        else if (ForecastListBoxItem.IsSelected)
        {
            PageTitle.Text = "Forecast";
            ContentFrame.Navigate(typeof(ForecastPage));
        }
        else if (SettingsListBoxItem.IsSelected)
        {
            PageTitle.Text = "Settings";
            ContentFrame.Navigate(typeof(SettingsPage));
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

问题属于SplitView的定义。

<SplitView Name="MySplitView" 
           Grid.Row="1" 
           DisplayMode="CompactInline" 
           OpenPaneLength="200" 
           CompactPaneLength="56" 
           HorizontalAlignment="Left">

由于您正在使用HorizontalAlignment="Left",因此您告诉SplitView“只获取适合我内容所需的宽度”。由于最初没有内容,因此它的大小将为200px,因为它是OpenPaneLength所需的宽度。

只需删除HorizontalAlignment(默认情况下为Stretch)。

<SplitView Name="MySplitView" 
           Grid.Row="1" 
           DisplayMode="CompactInline" 
           OpenPaneLength="200" 
           CompactPaneLength="56">

奖励:如果您想伪装选择更改事件以在初始加载时加载页面,则有几个选项。其中之一是在页面加载后选择一个项目:

    public MainPage()
    {
        this.InitializeComponent();
        this.Loaded += (sender, args) => IconsListBox.SelectedItem = IconsListBox.Items.First();
    }