2
We need to use the DependencyService
to implement the code for Android
and to iOS
.
First we will create and define our Interface
which will contain our return version name method:
public interface IDevice
{
string ConsultarVersao();
}
Now we will create a class in the project Android
and implement our interface which in this case is called IDevice
:
[assembly: Xamarin.Forms.Dependency(typeof(MeuApp.Droid.DeviceDroid))]
namespace MeuApp.Droid
{
public class DeviceDroid : IDevice
{
public string ConsultarVersao()
{
return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;
}
}
}
Now we’ll do the same for the iOS
, we will create a class in the project iOS
and implement our interface IDevice
:
[assembly: Xamarin.Forms.Dependency(typeof(MeuApp.iOS.DeviceIOS))]
namespace MeuApp.iOS
{
public class DeviceIOS : IDevice
{
public string ConsultarVersao()
{
return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
}
}
}
Okay, it’s all done. Now just use the method as follows that will work for both platforms:
DependencyService.Get<IDevice>().ConsultarVersao();
I hope I’ve helped.
I tried to use and it did not work, but thank you, with the other answer I got, thank you.
– Deivid Souza
Okay, I’m still sorry.
– SUR1C4T3
No, I appreciate your interest in helping.
– Deivid Souza