Je suis nouveau à Xamarin. J'ai le fragment de dialogue suivant:Xamarin. Comment montrer le dialogue dans une boucle?
public class EnableGpsDialog : DialogFragment
{
public static EnableGpsDialog newInstance()
{
EnableGpsDialog d = new EnableGpsDialog();
return d;
}
override public Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var builder = new AlertDialog.Builder(Activity);
var title = "Please, enable GPS";
builder.SetTitle(title);
builder.SetPositiveButton("OK", EnableGpsAction);
var dialog = builder.Create();
dialog.SetCanceledOnTouchOutside(false);
return dialog;
}
private void EnableGpsAction(object sender, DialogClickEventArgs e)
{
var intent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings);
StartActivity(intent);
}
}
Cela fonctionne bien, mais j'ai besoin que cette boîte de dialogue soit affichée lorsque gps est désactivé. Comment puis-je y parvenir? Mon approche (il ne montre rien):
private void EnableGps()
{
LocationManager locationManager = null;
while (true)
{
locationManager = (LocationManager)GetSystemService(Context.LocationService);
if (locationManager.IsProviderEnabled(LocationManager.GpsProvider))
{
return;//gps is enabled
}
else
{
//show dialog
EnableGpsDialog gpsDialog = new EnableGpsDialog();
var transaction = FragmentManager.BeginTransaction();
gpsDialog.Show(transaction, "Enable GPS dialog fragment");
}
}
}