Reputation: 780
I save some preferences in Application.Current.Properties for my Xamarin.Forms app and have a BroadcastReceiver that needs those preferences. The problem is that Xamarin.Forms is not initialized in the BroadcastReceiver so I need a way to access those variables in native Xamain.Android. Is that possible?
Upvotes: 0
Views: 248
Reputation: 780
I found the solution myself. I used the Xamarin.Forms source (found here) to write a function that reads the values from the same file as they are written to in the forms app. Works like a charm.
Upvotes: 0
Reputation: 15011
I need a way to access those variables in native Xamain.Android
you could use DependencyService ,this functionality enables Xamarin.Forms apps to do anything that a native app can do.
here is a sample for using Android's Toast to show message in shared code:
1.create a interface in shared code:
public interface IToast
{
void LongAlert(string message);//the parameter could pass to the native platform
void ShortAlert(string message);
}
2.Android Implementation:
[assembly: Dependency(typeof(ToastAndroid))]
namespace Demo.Droid
{
class ToastAndroid : IToast
{
public void LongAlert(string message)
{
Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long).Show();
}
public void ShortAlert(string message)
{
Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short).Show();
}
}
}
3.call in shared code:
DependencyService.Get<IToast>().ShortAlert("short toast);
DependencyService.Get<IToast>().LongAlert("long toast);
Upvotes: 0