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, "取消");

猜你喜欢

转载自blog.csdn.net/qq_21703215/article/details/105438744