【MAUI】控件


.NET Multi-platform App UI (.NET MAUI) 应用的用户界面 (UI) 由映射到每个目标平台的本机控件的对象构成。

用于创建 .NET MAUI 应用的用户界面的主控件组包括页面、布局和视图。 .NET MAUI 页面通常占据整个屏幕或窗口。 页面通常包含布局,其中包含视图以及可能的其他布局。 页面、布局和视图派生自 VisualElement 类。 此类提供可在派生类中发挥帮助作用的各种属性、方法和事件。

页面

.NET MAUI 应用由一个或多个页面组成。 页面通常占据整个屏幕或窗口,并且每个页面通常包含至少一个布局。

.NET MAUI 包含以下页面:

页面说明
ContentPageContentPage 显示单个视图,也是最常见的页面类型。
FlyoutPageFlyoutPage 页面用于管理两个相关信息页,一个为显示项的浮出控件页,另一个为详细信息页,显示浮出控件页上各项的详细信息。
NavigationPageNavigationPage 提供分层导航体验,用户可以根据需要向前或向后导航页面。
TabbedPageTabbedPage 由一系列页面组成,可通过页面顶部或底部的选项卡导航,每个选项卡都会加载页面内容。

在这里插入图片描述
ContentPage

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyMauiApp.MyPage"
             Title="MyPage"
             BackgroundColor="White">
    <StackLayout>
        <Label Text="Welcome to .NET MAUI!"
                VerticalOptions="Center"
                HorizontalOptions="Center" />
        <!-- Other views go here -->
    </StackLayout>
</ContentPage>

FlyoutPage

<FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:FlyoutPageNavigation"
            x:Class="FlyoutPageNavigation.MainPage">
    <FlyoutPage.Flyout>
        <local:FlyoutMenuPage x:Name="flyoutPage" />
    </FlyoutPage.Flyout>
    <FlyoutPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <local:ContactsPage />
            </x:Arguments>
        </NavigationPage>
    </FlyoutPage.Detail>
</FlyoutPage>

NavigationPage

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="NavigationPageTitleView.TitleViewPage">
    <NavigationPage.TitleView>
        <Slider HeightRequest="44"
                WidthRequest="300" />
    </NavigationPage.TitleView>
    ...
</ContentPage>

TabbedPage

<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:TabbedPageWithNavigationPage"
            x:Class="TabbedPageWithNavigationPage.MainPage">
    <local:TodayPage />
    <local:SchedulePage />
    <local:SettingsPage />
</TabbedPage>

布局

.NET MAUI 布局用于将用户界面控件组合到视觉结构中,每个布局通常包含多个视图。 布局类通常包含用于设置子元素位置和大小的逻辑。

.NET MAUI 包含以下布局:

Layout说明
AbsoluteLayoutAbsoluteLayout 将子元素放置在相对于其父元素的特定位置。
BindableLayoutBindableLayout 允许任何布局类通过绑定到项集合来生成其内容,并可以选择设置每个项的外观。
FlexLayoutFlexLayout 允许使用不同的对齐方式和方向选项堆叠或包裹其子元素。 FlexLayout 基于 CSS 灵活框布局模块,称为弹性布局或弹性框。
GridGrid 将其子元素放置在行和列网格中。
HorizontalStackLayoutHorizontalStackLayout 将子元素放置在水平堆栈中。
StackLayoutStackLayout 将子元素放置在垂直或水平堆栈中。
VerticalStackLayoutVerticalStackLayout 将子元素放置在垂直堆栈中。

在这里插入图片描述
AbsoluteLayout

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AbsoluteLayoutDemos.Views.XAML.StylishHeaderDemoPage"
             Title="Stylish header demo">
    <AbsoluteLayout Margin="20">
        <BoxView Color="Silver"
                 AbsoluteLayout.LayoutBounds="0, 10, 200, 5" />
        <BoxView Color="Silver"
                 AbsoluteLayout.LayoutBounds="0, 20, 200, 5" />
        <BoxView Color="Silver"
                 AbsoluteLayout.LayoutBounds="10, 0, 5, 65" />
        <BoxView Color="Silver"
                 AbsoluteLayout.LayoutBounds="20, 0, 5, 65" />
        <Label Text="Stylish Header"
               FontSize="24"
               AbsoluteLayout.LayoutBounds="30, 25" />
    </AbsoluteLayout>
</ContentPage>

BindableLayout

<StackLayout BindableLayout.ItemsSource="{Binding User.TopFollowers}"
             Orientation="Horizontal"
             ...>
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}"
                   Aspect="AspectFill"
                   WidthRequest="44"
                   HeightRequest="44"
                   ... />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

FlexLayout

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FlexLayoutDemos.Views.SimpleStackPage"
             Title="Simple Stack">    
    <FlexLayout Direction="Column"
                AlignItems="Center"
                JustifyContent="SpaceEvenly">        
        <Label Text="FlexLayout in Action"
               FontSize="18" />
        <Image Source="dotnet_bot_branded.png"
               HeightRequest="300" />
        <Button Text="Do-Nothing Button" />
        <Label Text="Another Label" />
    </FlexLayout>
</ContentPage>

Grid

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GridTutorial.MainPage">
    <Grid Margin="20,35,20,20">
        <Label Text="By default, a Grid contains one row and one column." />
    </Grid>
</ContentPage>

HorizontalStackLayout

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="StackLayoutDemos.Views.HorizontalStackLayoutPage">
    <HorizontalStackLayout Margin="20">
       <Rectangle Fill="Red"
                  HeightRequest="30"
                  WidthRequest="30" />
       <Label Text="Red"
              FontSize="18" />
    </HorizontalStackLayout>
</ContentPage>

StackLayout

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="StackLayoutDemos.Views.XAML.VerticalStackLayoutPage"
             Title="Vertical StackLayout demo">
    <StackLayout Margin="20">
        <Label Text="Primary colors" />
        <BoxView Color="Red"
                 HeightRequest="40" />
        <BoxView Color="Yellow"
                 HeightRequest="40" />
        <BoxView Color="Blue"
                 HeightRequest="40" />
        <Label Text="Secondary colors" />
        <BoxView Color="Green"
                 HeightRequest="40" />
        <BoxView Color="Orange"
                 HeightRequest="40" />
        <BoxView Color="Purple"
                 HeightRequest="40" />
    </StackLayout>
</ContentPage>

VerticalStackLayout

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="StackLayoutDemos.Views.VerticalStackLayoutPage">
    <VerticalStackLayout Margin="20">
        <Label Text="Primary colors" />
        <Rectangle Fill="Red"
                   HeightRequest="30"
                   WidthRequest="300" />
        <Rectangle Fill="Yellow"
                   HeightRequest="30"
                   WidthRequest="300" />
        <Rectangle Fill="Blue"
                   HeightRequest="30"
                   WidthRequest="300" />
        <Label Text="Secondary colors" />
        <Rectangle Fill="Green"
                   HeightRequest="30"
                   WidthRequest="300" />
        <Rectangle Fill="Orange"
                   HeightRequest="30"
                   WidthRequest="300" />
        <Rectangle Fill="Purple"
                   HeightRequest="30"
                   WidthRequest="300" />
    </VerticalStackLayout>
</ContentPage>

视图

.NET MAUI 视图是 UI 对象,例如标签、按钮和滑块,在其他环境中通常称为控件或小组件。

.NET MAUI 包含以下视图:

试图说明
ActivityIndicatorActivityIndicator 使用动画来说明应用正在进行耗时较长的活动,而不指示进度。
BlazorWebViewBlazorWebView 可用于在 .NET MAUI 应用中托管 Blazor Web 应用。
BorderBorder 是一个容器控件,用于围绕另一个控件绘制边框和/或背景。
BoxViewBoxView 可绘制具有指定宽度、高度和颜色的矩形或正方形。
ButtonButton 显示文本并响应点击或单击以指示应用执行任务。
CarouselViewCarouselView 可显示数据项的可滚动列表,用户在其中轻扫便可在集合中移动。
CheckBoxCheckBox 允许使用可以选中或留空的按钮类型选择布尔值。
CollectionViewCollectionView 使用不同的布局规范显示可选择数据项的可滚动列表。
ContentViewContentView 是一种支持创建自定义可重用控件的控件。
DatePickerDatePicker 允许使用平台日期选取器选择日期。
EditorEditor 可用于输入和编辑多行文本。
EllipseEllipse 显示省略号或圆圈。
EntryEntry 允许输入和编辑单行文本。
FrameFrame 用于使用可通过颜色、阴影及其他选项配置的边框来包装视图或布局。
GraphicsViewGraphicsView 是一个图形画布,可在上面使用 Microsoft.Maui.Graphics 命名空间中的类型绘制 2D 图形。
ImageImage 显示可从本地文件、URI、嵌入的资源或数据流加载的图像。
ImageButtonImageButton 显示图像并对指示应用执行任务的点击或单击操作做出响应。
IndicatorViewIndicatorView 显示表示 CarouselView 中的项数的指示器。
LabelLabel 显示单行和多行文本。
LineLine 显示从起点到终点的线条。 ListView ListView 显示可选择数据项的可滚动列表。
MapMap 显示地图,并且要求在应用中安装 Microsoft.Maui.Controls.Maps NuGet 包。
PathPath 显示曲线和复杂形状。
PickerPicker 显示项的简短列表,可从中选择项。
PolygonPolygon 显示多边形。
PolylinePolyline 显示一系列相互连接的直线。
ProgressBarProgressBar 使用动画来说明应用正在进行耗时较长的活动。
RadioButtonRadioButton 是一种按钮类型,允许从集中选择一个选项。
RectangleRectangle 显示矩形或正方形。
RefreshViewRefreshView 是一种容器控件,为可滚动内容提供下拉以刷新功能。
RoundRectangleRoundRectangle 显示带圆角的矩形或正方形。
ScrollViewScrollView 可滚动其内容,这通常是布局。
SearchBarSearchBar 是用于启动搜索的用户输入控件。
SliderSlider 允许从连续范围中选择 double 值。
StepperStepper 可以让你从一系列增量值中选择 double 值。
SwipeViewSwipeView 是一种容器控件,可环绕内容项,并提供通过轻扫手势显示的上下文菜单项。
SwitchSwitch 允许使用一种可打开或关闭的按钮类型选择布尔值。
TableViewTableView 显示可滚动项的表,这些项可分组到节中。
TimePickerTimePicker 允许使用平台时间选取器选择时间。
TwoPaneViewTwoPaneView 表示一个带有两个视图的容器,这些视图会调整内容的大小,并在可用空间内以并排方式或从上到下的顺序放置内容。
WebViewWebView 显示网页或本地 HTML 内容。

控件的常用样式属性,应该要非常熟悉,这些属性在很多控件上都能通用,如下所示:

1、布局类
背景:Background-背景、BackgroundColor-背景色、HasShadow-是否有阴影
边框:CornerRadius-圆角、BorderColor-边框颜色、BorderWidth-边框宽度外轮廓:Stroke-外轮廓画笔、StrokeShape-形状、StrokeThickness-宽度、StrokeDashArray-长短线样式、StrokeLineCap-起终点样式
宽高:WidthRequest-宽、HeightRequest-高、MaximumWidthRequest、MinimumWidthRequest等
对齐:VerticalOptions-垂直对齐、HorizontalOptions-水平对齐
间距:Margin-外边距,Padding-内边距

2、文本类
内容:Text-文内容
颜色:TextColor-文本颜色
字体:FontFamily-字体
大小:FontSize-字体大小
样式:FontAttributes-加粗斜体、TextDecorations-上划线下划线、TextTransform-大小写、FontAutoScalingEnabled-自动缩放
排列①:VerticalTextAlignment-文本垂直对齐、HorizontalTextAlignment-文本居中对齐排列②:CharacterSpacing-间距、LineHeight-行距、LineBreakMode-跨行、MaxLines-最大行

3、图片类
来源:Source-图片源、ImageSource-图片源
其它:IsLoading-加载、Aspect-缩放、IsAnimationPlaying-gif播放、IsOpaque-不透明

参考

官方文档
MAUI新生6.1:控件总览

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软泡芙

给爷鞠躬!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值