# maui开发系列
# xaml 页面控件
#### 通用属性(所有页面都可用)
属性名 | 说明 |
---|
AutomationId | 自动化测试标识 |
StyleClass | 样式类 |
Resources | 资源集合(样式、颜色等) |
BindingContext | 数据上下文 |
ToolbarItems | 工具栏按钮集合 |
Visual | 视觉样式(如 Material) |
FlowDirection | 布局方向(LTR/RTL) |
IsBusy | 忙碌状态 |
InputTransparent | 是否响应输入 |
Opacity | 透明度 |
IsEnabled | 是否可用 |
IsVisible | 是否可见 |
Margin | 外边距 |
WidthRequest/HeightRequest | 宽高请求 |
MinimumWidthRequest/MinimumHeightRequest | 最小宽高 |
MaximumWidthRequest/MaximumHeightRequest | 最大宽高 |
AnchorX/AnchorY | 锚点 |
Rotation/RotationX/RotationY | 旋转 |
Scale | 缩放 |
TranslationX/TranslationY | 平移 |
#### ContentPage 所有常用属性及注释
```
BackgroundColor="AliceBlue"
Padding="20,40,20,20"
IconImageSource="icon.png"
FlowDirection="LeftToRight"
IsBusy="False"
Visual="Material"
AutomationId="ContentPage1"
StyleClass="mainPage"
Shell.TabBarIsVisible="True"
Shell.NavBarIsVisible="True"
Shell.BackgroundColor="LightGray"
Shell.TitleColor="DarkBlue"
Shell.DisabledColor="Gray"
Shell.TabBarBackgroundColor="White"
Shell.TabBarTitleColor="Black"
Shell.TabBarUnselectedColor="LightGray"
Shell.TabBarForegroundColor="DodgerBlue"
Shell.TabBarDisabledColor="Gray"
Shell.TabBarShadowColor="LightGray"
>
```
#### FlyoutPage 所有常用属性及注释
```
IsPresented="False"
FlyoutIcon="menu.png"
FlyoutBackgroundColor="LightGray"
FlyoutWidth="300"
>
FlyoutHeader:菜单头部内容
FlyoutFooter:菜单底部内容
```
#### NavigationPage 所有常用属性及注释
```
BarTextColor="White"
TitleIconImageSource="navicon.png"
Shell.NavBarIsVisible="True"
Shell.NavBarHasShadow="True"
Shell.NavBarBackgroundColor="LightBlue"
Shell.NavBarTitleColor="DarkBlue"
Shell.NavBarDisabledColor="Gray"
Shell.NavBarForegroundColor="DodgerBlue"
Shell.NavBarShadowColor="LightGray"
>
HasNavigationBar(C# 设置):是否显示导航栏
```
#### TabbedPage 所有常用属性及注释
```
BarTextColor="Gray"
SelectedTabColor="DodgerBlue"
UnselectedTabColor="LightGray"
Shell.TabBarIsVisible="True"
Shell.TabBarBackgroundColor="White"
Shell.TabBarTitleColor="Black"
Shell.TabBarUnselectedColor="LightGray"
Shell.TabBarForegroundColor="DodgerBlue"
Shell.TabBarDisabledColor="Gray"
Shell.TabBarShadowColor="LightGray"
>
其它可用属性:
CurrentPage:当前选中页面(C# 访问)
每个子页面可自定义 Title、IconImageSource
```
#### Shell 介绍及与页面的关系
```
Shell 是 .NET MAUI 推荐的导航和应用结构容器,支持 Flyout(侧边栏)、TabBar(底部标签)、Stack(分层导航)等多种页面结构组合。
Shell 与四大页面的关系
Shell 可以包含 FlyoutPage、TabbedPage、NavigationPage、ContentPage。
Shell 通过 标签定义导航结构,页面通过 Route 路由注册。
Shell 属性(如 Shell.TabBarIsVisible)只有在 Shell 结构下才生效。
Shell 支持 URI 路由导航、全局样式、统一导航栏/TabBar配置。
```
#### **要点:**
```
Shell 可组合 Flyout(侧边栏)、TabBar(底部标签)、Stack(分层导航)。
Shell 支持全局样式和导航栏/TabBar统一配置。
页面可通过 Shell 属性自定义在 Shell 下的表现。
推荐新项目优先使用 Shell,结构更清晰、导航更强大。
```
# xaml 布局控件
#### StackLayout
属性名 | 说明 |
---|
Orientation | 布局方向(Vertical/Horizontal) |
Spacing | 子元素间距 |
Padding | 内边距 |
Margin | 外边距 |
HorizontalOptions | 水平对齐方式 |
VerticalOptions | 垂直对齐方式 |
BackgroundColor | 背景色 |
#### Grid
属性名 | 说明 |
---|
RowDefinitions | 行定义(高度) |
ColumnDefinitions | 列定义(宽度) |
Padding | 内边距 |
Margin | 外边距 |
RowSpacing | 行间距 |
ColumnSpacing | 列间距 |
BackgroundColor | 背景色 |
```
子元素特有属性:
Grid.Row:所在行
Grid.Column:所在列
Grid.RowSpan:跨行数
Grid.ColumnSpan:跨列数
```
#### FlexLayout
属性名 | 说明 |
---|
Direction | 主轴方向(Row/Column) |
Wrap | 是否换行(NoWrap/Wrap/Reverse) |
JustifyContent | 主轴对齐方式 |
AlignItems | 交叉轴对齐方式 |
AlignContent | 多行对齐方式 |
Padding | 内边距 |
Margin | 外边距 |
BackgroundColor | 背景色 |
```
子元素特有属性:
FlexLayout.Order:排序权重
FlexLayout.Grow:放大比例
FlexLayout.Shrink:缩小比例
FlexLayout.Basis:主轴初始大小
FlexLayout.AlignSelf:单独对齐方式
```
#### AbsoluteLayout
属性名 | 说明 |
---|
Padding | 内边距 |
Margin | 外边距 |
BackgroundColor | 背景色 |
```
子元素特有属性:
AbsoluteLayout.LayoutBounds:位置和尺寸(如 "x, y, width, height")
AbsoluteLayout.LayoutFlags:尺寸和位置的解释方式(All/None/WidthProportional/HeightProportional等)
```
#### VerticalStackLayout / HorizontalStackLayout
性能更优的垂直/水平堆叠布局
属性名 | 说明 |
---|
Spacing | 子元素间距 |
Padding | 内边距 |
Margin | 外边距 |
HorizontalOptions | 水平对齐方式 |
VerticalOptions | 垂直对齐方式 |
BackgroundColor | 背景色 |
#### ContentView
属性名 | 说明 |
---|
Content | 内部内容 |
Padding | 内边距 |
Margin | 外边距 |
BackgroundColor | 背景色 |
#### 通用布局属性
```
HorizontalOptions、VerticalOptions:对齐方式(Start、Center、End、Fill)
Margin、Padding:外边距、内边距
BackgroundColor:背景色
IsVisible、IsEnabled、Opacity:可见性、可用性、透明度
WidthRequest、HeightRequest:宽高请求
```
# xaml 进阶控件
#### ScrollView
**作用**:让内容可滚动(支持垂直或水平滚动)。
**常用属性:**
- `Orientation`:滚动方向(Vertical/Horizontal)
- `Content`:内部内容
- `Padding`、`Margin`、`BackgroundColor`
---
#### CollectionView
**作用**:高性能列表/网格布局,支持数据绑定、分组、选择等。
**常用属性:**
- `ItemsSource`:数据源
- `ItemTemplate`:单元格模板
- `ItemsLayout`:布局方式(垂直列表、水平列表、网格)
- `SelectionMode`、`SelectedItem`
- `Header`、`Footer`
#### CarouselView
**作用**:轮播图、滑动页面。
**常用属性:**
- `ItemsSource`、`ItemTemplate`
- `IsBounceEnabled`、`PeekAreaInsets`
- `Loop`、`Position`
#### Frame
**作用**:带圆角、阴影的内容容器,常用于卡片、面板。
**常用属性:**
- `CornerRadius`:圆角半径
- `HasShadow`:是否有阴影
- `BorderColor`、`BackgroundColor`
- `Content`
#### Border(.NET MAUI 新增)
**作用**:为内容添加边框和圆角。
**常用属性:**
- `Stroke`:边框颜色
- `StrokeThickness`:边框宽度
- `StrokeShape`:边框形状(圆角、椭圆等)
- `Background`、`Content`
#### ContentPresenter
**作用**:用于自定义控件模板时占位显示内容。
**常用属性:**
- `Content`:要显示的内容
#### Expander
**作用**:可展开/收起的内容区域。
# xaml 视图控件
#### 1. Label
```