UWP控件——SplitView的使用

UWP控件——SplitView的使用

1. 控件结构

SplitView菜单常用于抽屉式菜单展示和功能导航,该控件由两部分构成:

  • 导航面板:SplitView.Pane
  • 内容部分:SplitView.Content

导航面板用于功能展示,例如下图左侧黑色部分,右侧白色即为内容部分。这两部分内容缺一不可。

在这里插入图片描述

2. 显示模式

SplitView布局控件的展示模式(DisplayMode)分为四种:

  • Inline:将右侧内容直接推开

在这里插入图片描述

  • Overlay:覆盖右侧内容,覆盖的宽度取决于OpenPaneLength设置的像素值

在这里插入图片描述

  • CompactOverlay:通过设置CompactPaneLength,预留一部分宽度,一般是为了展示图标信息,如下图所示,这里同样会对右侧的SplitView.Content进行遮盖。

在这里插入图片描述

  • CompactInline:同样需要设置CompactPaneLength来预留宽度,面板打开后会将右侧内容推开。

3. 简单Demo

了解完上述基础知识,下面简单做个Demo来试用下SplitView

  • 通过鼠标点击事件,控制左侧SplitView.Pane面板的开关。XAML设计代码如下:
<Page
    x:Class="basicSplitView.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:basicSplitView"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <SplitView x:Name="mySplitView" DisplayMode="Overlay" CompactPaneLength="100" OpenPaneLength="200" IsPaneOpen="True">
            <SplitView.Pane>
                <StackPanel Background="Gold">
                    <TextBlock Text="helo"/>
                    <TextBlock Text="heoj"/>
                    <TextBlock Text="jfhs"/>
                </StackPanel>
            </SplitView.Pane>
            <SplitView.Content>
                <StackPanel Background="LightGreen">
                    <TextBlock Text="jhfa"/>
                    <TextBlock Text="jjia"/>
                    <TextBlock Text="kfoa"/>
                </StackPanel>
            </SplitView.Content>
        </SplitView>
        <Button Content="Click Me" Click="Button_Click" HorizontalAlignment="Center"/>
    </Grid>
</Page>

后台的C#代码为:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace basicSplitView
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            mySplitView.IsPaneOpen = !mySplitView.IsPaneOpen;
        }
    }
}

最终效果为:

  • 默认界面
    在这里插入图片描述
  • 点击按钮后
    在这里插入图片描述
    在实现汉堡菜单时,需要使用上述SplitView控件。而现在,对于汉堡菜单,有全新的NavigationView控件,实现了汉堡菜单的基本功能,界面如下:
    在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinUI 控件UWP 控件、WPF 控件和 Silverlight 控件在语法和结构上有所不同,因此可以通过检查 XAML 代码的命名空间来区分它们。以下是一些常见的命名空间和控件: - WinUI 控件:命名空间为 `http://schemas.microsoft.com/winui/2021/xaml/behaviors` 或 `http://schemas.microsoft.com/winui/2021/xaml/presentation`,控件名称以 `Microsoft.UI` 开头。 - UWP 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/winfx/2008/xaml/presentation`,控件名称以 `Windows.UI` 开头。 - WPF 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/netfx/2007/xaml/presentation`,控件名称以 `System.Windows` 或 `Microsoft.Windows` 开头。 - Silverlight 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/client/2007`,控件名称以 `System.Windows.Controls` 或 `Microsoft.Windows.Controls` 开头。 可以通过读取 XAML 文件中的命名空间来确定使用控件类型。例如,以下代码片段演示了如何读取 XAML 文件中的命名空间: ```csharp using System.Xml.Linq; // Load XAML file into an XDocument XDocument xdoc = XDocument.Load("MyXamlFile.xaml"); // Get the root element of the XAML file XElement root = xdoc.Root; // Get the default namespace of the XAML file XNamespace ns = root.GetDefaultNamespace(); // Check the namespace to determine the type of controls used in the XAML file if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/winui")) { // WinUI controls } else if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/winfx")) { // UWP or WPF controls } else if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/client")) { // Silverlight controls } else { // Unknown namespace } ``` 请注意,这只是一种简单的方法来区分不同类型的控件,实际上还需要考虑一些其他因素,例如控件的属性和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值