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

相关推荐
试行24 分钟前
C#System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误。
c#
动亦定30 分钟前
AI与物联网(IoT)的融合
人工智能·物联网
EngZegNgi42 分钟前
安卓应用启动崩溃的问题排查记录
android·crash·启动崩溃
智想天开1 小时前
31.设计模式的反模式与常见误区
设计模式
火柴就是我1 小时前
每日见闻之Container Decoration
android·flutter
天枢破军1 小时前
【AOSP】解决repo拉取提示无法连接android.googlesource.com
android
whysqwhw2 小时前
OkHttp之AndroidPlatform类分析
android
XiaolongTu2 小时前
Kotlin Flow详述:从一个“卡顿”问题到线程切换的本质
android·面试
Kapaseker2 小时前
全网最详细的Compose Stable讲解,你一定要看
android
solo_992 小时前
使用Android Studio 聊微信
android