uno-platform - Uno主布局
问题描述
我是 Uno 的新手,我一直在关注框架导航教程。我注意到当框架导航整个窗口时会发生变化。这很好,但不是最优的。Uno 中有没有一种方法可以拥有主布局,就像您在 ASP.Net MVC 项目中看到的那样?我宁愿不在每个页面上实现导航菜单。
解决方案
要扩展@matfillion 的答案,如果NavigationView
不适合您的需求,您可以轻松滚动自己的导航外壳,同时利用内置框架导航。不需要Frame
成为应用程序中的顶级控件。
下面是一个超简单的例子,来说明原理。在页面之间导航时,导航列表将保持在顶部可见。
Shell.xaml:
<UserControl x:Class="UnoTestbed44.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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>
<ListView x:Name="NavigationList"
Background="LightGray"
ItemsSource="{x:Bind Pages}"
Grid.Row="0"
DisplayMemberPath="Label"
SelectionChanged="NavigationList_SelectionChanged">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<Frame x:Name="MainFrame"
Grid.Row="1" />
</Grid>
</UserControl>
Shell.xaml.cs:
using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
namespace UnoTestbed44
{
public sealed partial class Shell : UserControl
{
public NavigationItem[] Pages { get; } = new[] {
new NavigationItem {Label = "First page", PageType = typeof(Page1)},
new NavigationItem {Label = "Second page", PageType = typeof(Page2)},
};
public Shell()
{
this.InitializeComponent();
}
private void NavigationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.FirstOrDefault() is NavigationItem navigationItem)
{
MainFrame.Navigate(navigationItem.PageType);
}
}
public class NavigationItem
{
public string Label { get; set; }
public Type PageType { get; set; }
}
}
}
OnLaunched()
在 App.xaml.cs 中重写:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if NET5_0 && WINDOWS
_window = new Window();
_window.Activate();
#else
_window = Windows.UI.Xaml.Window.Current;
#endif
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (_window.Content == null)
{
_window.Content = new Shell();
}
#if !(NET5_0 && WINDOWS)
if (e.PrelaunchActivated == false)
#endif
{
// Ensure the current window is active
_window.Activate();
}
}
推荐阅读
- python - 以 10 为底的 int() 的 Python 3 无效文字:“开始”
- python-3.x - 如何在 Python 3 中使用输入值编写 fizzbuzz 函数?
- python - Python:选择一个大数作为初始最大值
- c# - wix 想要一个程序集清单 - 我有一个
- php - 检查 Laravel 对象是否有项目
- angular - 角度材料matpaginator不显示数据
- python - 无法使用 Django Rest Framework 保存两个对象之间的关系
- python - 关于python3导入包的问题
- javascript - 如何将 require/import 视为一个值而不是依赖项
- apache-spark - Pyspark:如何从 Weeknumber 和 Year 获取日期