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

본문 바로가기
사이트 내 전체검색

APP Dev

[Xamarin Interface - IOS/Android] Example

페이지 정보

작성자 sbLAB 댓글 0건 조회 157회 작성일 19-10-19 09:36

본문

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();



댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
3
어제
21
최대
91
전체
7,675

그누보드5
Copyright © sebom.com All rights reserved.