MAUI学习笔记(三)-视图绑定模型
一、创建实体模型-About.cs
using System;
namespace Test.Models
{
/// <summary>
/// 关于页
/// </summary>
public class About
{
/// <summary>
/// App标题
/// </summary>
public string Title=> AppInfo.Name;
/// <summary>
/// App版本
/// </summary>
public string Version => AppInfo.VersionString;
/// <summary>
/// 信息
/// </summary>
public string Text => "Holle World!";
}
}
二、创建视图-AboutPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:Test.Models"
x:Class="Test.Views.AboutPage"
Title="关于页">
<ContentPage.BindingContext>
<models:About />
</ContentPage.BindingContext>
<VerticalStackLayout>
<HorizontalStackLayout Spacing="10">
<Image Source="dotnet_bot.png"
SemanticProperties.Description="你好!"
HeightRequest="64" />
<Label FontSize="22" FontAttributes="Bold" Text="{Binding Title}" VerticalOptions="End" />
<Label FontSize="22" Text="{Binding Version}" VerticalOptions="End" />
</HorizontalStackLayout>
<Label Text="{Binding Text}" />
</VerticalStackLayout>
</ContentPage>
1、AboutPage.xaml视图页引用模型类所在的路径:xmlns:models="clr-namespace:Test.Models"
;
2、AboutPage.xaml根元素ContentPage
添加BindingContext
属性用来绑定数据:
<ContentPage.BindingContext>
<models:About />
</ContentPage.BindingContext>
3、使用 {Binding ..}
来给控件的属性绑定实体中的数据,例如:<Label Text="{Binding Version}" />
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/16709742.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下