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

本文介绍了如何在Xamarin.Forms构建的Android应用中,使用Xamarin.Essentials的VersionTracking类获取并显示当前版本号,以创建一个简单的AboutPage。
摘要由CSDN通过智能技术生成

代码实现

在构建 Android App 时,写了一个 AboutPage。在 AboutPage 上显示 App 的当前版本号是常见的做法。使用 Xamarin.Foms 获取当前版本号的方法是使用 Xamarin.Forms 的 VersionTracking 类。
如下,我写了一个非常简单的 AboutPage,其中定义了一个Span,命名为 spText,用于显示 App 的当前版本号。

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 如下:

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

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

硬核老骆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值