图片是盗用别人的,主要是为了更直观点。

ICommand 主要处理页面点击事件,看过前面MVVM 模式的应该知道是什么回事

使用CollectionView 控件做为其他子控件的容器,该控件有一个 ItemsSource 属性,把它绑定到定义的子项数据源









它会根据传进去的item ,返回DataTemplate 回来页面,所以就把占位的DataTemplate 去掉。

以上处理完成后,当页面调用这个模板选择器的时候,就会去页面找到定义模板并呈现内容出来。

- <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:s="clr-namespace:MauiApp1.Models"
- x:Class="MauiApp1.MainPage" Background="LightGray">
- <ContentPage.Resources>
- <Style TargetType="RadioButton" x:Key="NavButtonStyle">
- <Setter Property="WidthRequest" Value="70"/>
- <Setter Property="HeightRequest" Value="90"/>
- <Setter Property="TextColor" Value="White"/>
- <Setter Property="Margin" Value="5,10"/>
- <Setter Property="FontSize" Value="40"/>
- <Setter Property="VerticalOptions" Value="Center"/>
- <Setter Property="HorizontalOptions" Value="Center"/>
- <Setter Property="ControlTemplate">
- <ControlTemplate>
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition />
- <RowDefinition Height="30" />
- Grid.RowDefinitions>
- <Rectangle Fill="Yellow"
- Stroke="#8de9ff" StrokeThickness="1"
- RadiusX="20" RadiusY="20" WidthRequest="50" HeightRequest="50"
- IsVisible="{TemplateBinding IsVisible}"/>
- <Label Text="{TemplateBinding Value}" FontFamily="Iconfont" FontSize="30"
- TextColor="{TemplateBinding TextColor}"
- HorizontalOptions="Center"
- VerticalOptions="Center">Label>
- <Label Text="{TemplateBinding Content}" Grid.Row="1"
- HorizontalOptions="Center"
- VerticalOptions="Center"
- TextColor="White"
- FontSize="13">Label>
- Grid>
- ControlTemplate>
- Setter>
- <Style.Triggers>
- <Trigger TargetType="RadioButton" Property="IsChecked" Value="True">
- <Setter Property="TextColor" Value="Blue"/>
- Trigger>
- Style.Triggers>
- Style>
-
-
- <DataTemplate x:Key="DI1">
- <Label Text="{Binding Header}" TextColor="Red"/>
- DataTemplate>
- <DataTemplate x:Key="DI2">
- <Label Text="{Binding Header} " TextColor="Yellow"/>
- DataTemplate>
- <DataTemplate x:Key="DI3">
- <Label Text="{Binding Header}" TextColor="Green"/>
- DataTemplate>
- ContentPage.Resources>
- <Grid Margin="0,30,0,0">
- <Grid.RowDefinitions>
- <RowDefinition Height="40"/>
- <RowDefinition Height="120"/>
- <RowDefinition />
- Grid.RowDefinitions>
- <Grid Margin="10">
- <Label Text="" FontFamily="Iconfont" VerticalOptions="Center" TextColor="White"/>
- <HorizontalStackLayout VerticalOptions="Center" HorizontalOptions="Center">
- <Label Text="一楼客厅" TextColor="White"/>
- <Label Text="" FontFamily="Iconfont" TextColor="White" VerticalOptions="Center" FontSize="10"/>
- HorizontalStackLayout>
- Grid>
- <ScrollView HorizontalScrollBarVisibility="Never" Grid.Row="1">
- <CollectionView ItemsSource="{Binding NavList}" >
- <CollectionView.ItemsLayout>
- <LinearItemsLayout Orientation="Horizontal"/>
- CollectionView.ItemsLayout>
- <CollectionView.ItemTemplate>
- <DataTemplate>
- <RadioButton Content="{Binding Text}" Value="{Binding Icon}" Style="{StaticResource NavButtonStyle}">RadioButton>
- DataTemplate>
- CollectionView.ItemTemplate>
- CollectionView>
- ScrollView>
- <CollectionView Grid.Row="2" ItemsSource="{Binding LighConrrols}">
- <CollectionView.ItemTemplate>
- <s:LightItemTemplateSelector DI1="{StaticResource DI1}" DI2="{StaticResource DI2}" DI3="{StaticResource DI3}" />
- CollectionView.ItemTemplate>
- CollectionView>
- Grid>
- ContentPage>
出自:全网首发.NET MAUI框架入门 已完结 附企业级WPF实战(C#/blazor/xamarin/停车场项目+通用框架+数据采集与监控/数据库)B0858_哔哩哔哩_bilibili
仅个人学习记录