在通用項目中有時候需要用到安卓或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, "取消");