[Xamarin Interface - IOS/Android] Example > APP Dev

본문 바로가기

사이트 내 전체검색

APP Dev

[Xamarin Interface - IOS/Android] Example

작성일 19-10-19 09:36

페이지 정보

작성자sbLAB 조회 2,441회 댓글 0건

본문

Some example code:

https://forums.xamarin.com/discussion/104922/how-to-set-get-the-app-version-number-and-build-for-cross-platform 



In your PCL define an interface like this:


namespace Your.Namespace.Interfaces

{

    public interface IAppVersion

    {

        string GetVersion();

        int GetBuild();

    }

}


And implement it like this in your platform projects:


IOS:


[assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.iOS.Version_iOS))]

namespace Your.Namespace.iOS

{

    public class Version_iOS : IAppVersion

    {

        public string GetVersion()

        {

            return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();

        }

        public int GetBuild()

        {

            return int.Parse(NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString());

        }

    }

}


Android:


[assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.Droid.Version_Android))]

namespace Your.Namespace.Droid

{

    public class Version_Android : IAppVersion

    {

        public string GetVersion()

        {

            var context = global::Android.App.Application.Context;


            PackageManager manager = context.PackageManager;

            PackageInfo info = manager.GetPackageInfo(context.PackageName, 0);


            return info.VersionName;

        }


        public int GetBuild()

        {

            var context = global::Android.App.Application.Context;

            PackageManager manager = context.PackageManager;

            PackageInfo info = manager.GetPackageInfo(context.PackageName, 0);


            return info.VersionCode;

        }

    }

}


You can use this then in your pcl code:


string v = DependencyService.Get<IAppVersion>().GetVersion();

int b = DependencyService.Get<IAppVersion>().GetBuild();



댓글목록

등록된 댓글이 없습니다.

Copyright © 소유하신 도메인. All rights reserved.
PC 버전으로 보기