我需要在设置页面中显示应用的构建版本。
那么如何以编程方式从versionCode
文件中获取versionName
和AndroidManifest.xml
。
或者
有没有办法在xamarin.forms
中以编程方式获取构建版本。
答案 0 :(得分:29)
使用依赖服务:
对于iOS:
using Xamarin.Forms;
[assembly: Dependency(typeof(ScreenAndroid))]
namespace XXX.Droid
{
class ScreenAndroid : Java.Lang.Object, IScreen
{
public string Version
{
get
{
var context = Forms.Context;
return context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
}
}
}
}
对于Android:
interface IScreen
{
string Version { get; }
}
界面:
{{1}}
答案 1 :(得分:22)
对于版本名称:
Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName
对于版本代码:
Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionCode
答案 2 :(得分:1)
在Xamarin.Android
中,您可以随时尝试:
public double GetAppVersion()
{
var info = AppContext.PackageManager.GetPackageInfo(AppContext.PackageName, 0);
return info.VersionCode;
}
然后您需要使用dependency injection从Xamarin.Forms
项目中调用此方法。
答案 3 :(得分:0)
在Xamarin.Essentials中,您可以使用以下内容:
SELECT id,city,city_url,
(SELECT count(shop) FROM shops JOIN
shop_categories ON shop_categories.shop_id = shops.id WHERE
city_id=cities.id && shop_categories.category_id = :catId LIMIT 1) as
totalshops,
(6371 * 2 * ASIN(SQRT( POWER(SIN(( :latitude - latitude) *
pi()/180 / 2), 2) +COS( :latitude * pi()/180) * COS( :latitude *
pi()/180) * POWER(SIN(( :longitude - longitude) * pi()/180 / 2), 2) )))
as distance
from cities
having totalshops > 1
order by distance ASC
LIMIT 1
从那里可以获得构建版本。
如果您真的只想要内部版本号,也可以使用快捷方式:
Version version = AppInfo.Version;