我整天都在搜索和寻找示例,但找不到解决我的问题的方法。我有一个 SplitView,它的 Pane 中有一个汉堡包菜单,我正在为每个选定的 ListBoxItems 加载一个框架。但是当我的页面加载时,它看起来像这样(橙色区域是页面背景): 我还想知道如何将我的任何框架设置为在启动时加载,我尝试将 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="" 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="" />
<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="" />
<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="" />
<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));
}
}
}
}
最佳答案
问题出在您的 SplitView
的定义范围内。
<SplitView Name="MySplitView"
Grid.Row="1"
DisplayMode="CompactInline"
OpenPaneLength="200"
CompactPaneLength="56"
HorizontalAlignment="Left">
因为您使用的是 HorizontalAlignment="Left"
,所以您是在告诉 SplitView“只采用适合我的内容所需的宽度”。由于最初没有内容,它将调整为 200 像素的宽度,因为这是 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();
}
关于c# - 为什么我的 SplitView 的内容和 Pane 在同一区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40685327/