首页 > 解决方案 > Uno主布局

问题描述

我是 Uno 的新手,我一直在关注框架导航教程。我注意到当框架导航整个窗口时会发生变化。这很好,但不是最优的。Uno 中有没有一种方法可以拥有主布局,就像您在 ASP.Net MVC 项目中看到的那样?我宁愿不在每个页面上实现导航菜单。

标签: uno-platform

解决方案


要扩展@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();
            }
        }

推荐阅读