Xamarin.Forms菜鳥筆記--5.調用平臺特有功能

在通用項目中有時候需要用到安卓或ios特有的功能,或通用平臺插件不夠用
這時可以在form項目中新建一個接口,直接代碼

namespace Phenix.Services.Helper
{
    public interface IPlatformService
    {
        string GetAppVersion();
    }
}

然後在安卓中實現

using Phenix.Services.Helper;
using Xamarin.Forms;

[assembly: Dependency(typeof(Phenix.Droid.Video.ImpDroidService))]
namespace Phenix.Droid.Video
{
    public class ImpDroidService: IPlatformService
    {
        public string GetAppVersion()
        {
            return "andorid";
        }
    }
}

在ios中實現

using Phenix.Services.Helper;
using Xamarin.Forms;

[assembly: Dependency(typeof(Phenix.iOS.Video.ImpiOSService))]
namespace Phenix.iOS.Video
{
    public class ImpiOSService: IPlatformService
    {
        public string GetAppVersion()
        {
            return "ios";
        }
    }
}

form中使用

Services.Helper.IPlatformService _ps = DependencyService.Get<Services.Helper.IPlatformService>();
string msg = _ps.GetAppVersion();
DisplayAlert("平臺", msg, "取消");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章