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

相关推荐
唐青枫1 小时前
C#.NET FluentValidation 全面解析:优雅实现对象验证
c#·.net
不吃凉粉8 小时前
Android Studio USB串口通信
android·ide·android studio
zhangphil8 小时前
android studio设置大内存,提升编译速度
android·android studio
编程乐学10 小时前
安卓非原创--基于Android Studio 实现的天气预报App
android·ide·android studio·课程设计·大作业·天气预报·安卓大作业
从孑开始10 小时前
ManySpeech.MoonshineAsr 使用指南
人工智能·ai·c#·.net·私有化部署·语音识别·onnx·asr·moonshine
Deschen11 小时前
设计模式-外观模式
java·设计模式·外观模式
YuanlongWang11 小时前
C# 中,依赖注入(DI)的实现方式
c#
大熊的瓜地11 小时前
Android automotive 框架
android·android car
私人珍藏库12 小时前
[Android] Alarm Clock Pro 11.1.0一款经典简约个性的时钟
android·时钟
SmartSoftHelp开发辅助优化12 小时前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#