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 跨平台开发的资料实在是太少了,我这里就先加一点小砖头先。

相关推荐
Small black human5 小时前
设计模式-应用分层
设计模式
雨白8 小时前
Jetpack系列(二):Lifecycle与LiveData结合,打造响应式UI
android·android jetpack
星源~8 小时前
tree 命令集成到 Git Bash:可视化目录结构的指南
git·单片机·物联网·嵌入式·项目开发
葬歌倾城9 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
kk爱闹10 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
Eiceblue10 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王10 小时前
hello判断
开发语言·c#
大美B端工场-B端系统美颜师11 小时前
多模态数据融合预警:从IoT传感器到卫星监测的可视化方案升级
物联网·交互·数据预警
每次的天空11 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
恋猫de小郭12 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios