PLC-IoT 网关开发札记(2):Xamarin Forms 工程获取App当前的版本号

代码实现

在构建 Android App 时,写了一个 AboutPage。在 AboutPage 上显示 App 的当前版本号是常见的做法。使用 Xamarin.Foms 获取当前版本号的方法是使用 Xamarin.Forms 的 VersionTracking 类。

如下,我写了一个非常简单的 AboutPage,其中定义了一个Span,命名为 spText,用于显示 App 的当前版本号。

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace I2oT.Views.SystemSettings
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class AboutPage : ContentPage
	{
		public AboutPage ()
		{
			InitializeComponent ();
		}

        protected override void OnAppearing()
        {
            base.OnAppearing();

            spVersion.Text = "   v" + VersionTracking.CurrentVersion;
        }

        async void OnButtonClicked(object sender, EventArgs e)
        {
            await Launcher.OpenAsync("https://aka.ms/xamarin-quickstart");
        }
    }
}

XAML 如下:

cs 复制代码
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:i2ot="clr-namespace:I2oT"
             x:DataType="i2ot:AppShell"
             x:Class="I2oT.Views.SystemSettings.AboutPage"
             Title="关于">

    <Grid RowDefinitions="Auto,Auto,*"
          ColumnDefinitions="0.25*,*"
          Padding="8">
        <Image Grid.RowSpan="2" Grid.Column="0" 
               Source="icon_app.png" 
               BackgroundColor="{StaticResource AppForegroundColor}"
               Opacity="0.9"
               VerticalOptions="StartAndExpand"
               HeightRequest="48"/>

        <Label Grid.Column="1" 
               FontSize="16"
               HorizontalOptions="FillAndExpand" 
               HorizontalTextAlignment="Start"
               VerticalOptions="CenterAndExpand">
            <Label.FormattedText>
                <FormattedString>
                    <FormattedString.Spans>
                        <Span Text="智能IoT平台"
                              TextColor="{StaticResource AppTextCommonColor}"
                              FontAttributes="Normal"
                              FontSize="Medium"/>
                        <Span x:Name="spVersion" 
                              Text="  V1.0.3"
                              TextColor="{StaticResource AppTextCommonColor}"/>
                    </FormattedString.Spans>
                </FormattedString>
            </Label.FormattedText>
        </Label>

        <Label Grid.Row="1" Grid.Column="1"
               Text="Intelligent IoT Flatform"
               TextColor="{StaticResource AppTextCommonColor}"
               FontSize="16"/>
        
        <StackLayout Grid.ColumnSpan="2"
                     Grid.Row="2" Grid.Column="0"
                     Margin="0,64,0,0">
            <StackLayout.Resources>
                <Style x:Key="copyrightStyle" TargetType="Label">
                    <Setter Property="TextColor" Value="Black"/>
                    <Setter Property="FontSize" Value="Caption"/>
                    <Setter Property="VerticalOptions" Value="Start"/>
                    <Setter Property="HorizontalTextAlignment" Value="Start"/>
                </Style>
            </StackLayout.Resources>
            
            <Label Text="基于 Xamarin.Forms,使用XAML和C#编程."
                   TextColor="{StaticResource AppTextCommonColor}"
                   HorizontalTextAlignment="Center"/>
            
            <Button Text="了解更多"
                    FontSize="Caption" 
                    HeightRequest="40" 
                    Margin="16,0" 
                    CharacterSpacing="0.5"
                    Clicked="OnButtonClicked" />
            
            <Label Text="Copyright(c) 2023-2024 CuteModem Intelligence."
                   Style="{StaticResource copyrightStyle}"
                   VerticalOptions="EndAndExpand"/>

            <Label Text="All rights reserved."
                   Style="{StaticResource copyrightStyle}"/>
        </StackLayout>
    </Grid>
</ContentPage>

屏幕截图如下:

贴了这么多代码,干货只有一条语句:

spVersion.Text = " v" + VersionTracking.CurrentVersion;

网上有一些话题也提到这个需求,有不少答案,但亲测不行。

VersionTracking 类

MSDN上对VersionTracking类有简要的说明,参见网址:

VersionTracking 类 (Xamarin.Essentials) | Microsoft Learn

VersionTracking 类是一个考虑较为周详的静态类,其中 IsFirstLaunchxxx 对应用程序的初始化有很大的帮助。

网上关于 Xamarin.Forms 跨平台开发的资料实在是太少了,我这里就先加一点小砖头先。

相关推荐
雨白20 小时前
Jetpack Compose Navigation 2.x 详解
android·android jetpack
大G的笔记本1 天前
Java常见设计模式面试题(高频)
java·开发语言·设计模式
老鼠只爱大米1 天前
Java设计模式之建造者模式(Builder)详解
java·设计模式·建造者模式·builder·23种设计模式
飞睿科技1 天前
【芯片选型指南】乐鑫ESP32-C61核心能力解析:为何它在Wi-Fi 6物联网赛道中优势独具?
科技·嵌入式硬件·物联网·智能家居
AcrelZYL1 天前
古建筑用电安全如何守护,安科瑞ASCB3智能微型断路器
物联网·安全
Android系统攻城狮1 天前
Android内核进阶之获取DMA地址snd_pcm_sgbuf_get_addr:用法实例(九十一)
android·pcm·android内核·音频进阶·pcm硬件参数
清风与日月1 天前
c# 集成激光雷达(以思岚A1为例)
开发语言·c#
清空mega1 天前
Android Studio移动应用基础教程(前言)
android·ide·android studio
无极小卒1 天前
如何在三维空间中生成任意方向的矩形内部点位坐标
开发语言·算法·c#
2501_937145411 天前
2025IPTV 源码优化版实测:双架构兼容 + 可视化运维
android·源码·源代码管理·机顶盒