Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisplayAlert With changing Text xamarin forms

I have a requirement where i have to show the status of the download on a DisplayAlert. But with changing text on it asynchronously.

How to achieve this?

  DisplayAlert("Download Info", "Downloading.....", "Ok");

I want to show status like...

  • Connected to server
  • Downloading
  • Download Complete
like image 757
DEV Avatar asked Apr 24 '17 00:04

DEV


People also ask

What is NavigationPage in Xamarin forms?

The NavigationPage class provides a hierarchical navigation experience where the user is able to navigate through pages, forwards and backwards, as desired. The class implements navigation as a last-in, first-out (LIFO) stack of Page objects.

Is Xamarin forms deprecated?

In May 2020, Microsoft announced that Xamarin. Forms, a major component of its mobile app development framework, would be deprecated in November 2021 in favour of a new . Net based product called MAUI - Multiform App User Interface.


1 Answers

Here is a simple "Dynamic Alert" for Forms and iOS using UIAlertController and Android using a DialogFragment and a Xamarin.Forms dependency service:

Dependency Interface:

public interface IDynamicAlert
{
    void Show(string title, string message);
    void Update(string message);
    void Dismiss();
}

iOS IDynamicAlert Dependency Implementation:

public class DynamicAlert : IDynamicAlert
{
    UIAlertController alert;

    public void Show(string title, string message)
    {
        if (alert != null) throw new Exception("DynamicAlert already showing");
        alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
        var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
        rootVC.PresentViewController(alert, true, () =>
        {
        });
    }

    public void Update(string message)
    {
        if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");
        alert.Message = message;
    }

    public void Dismiss()
    {
        if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");
        alert.DismissViewController(true, () =>
        {
            alert.Dispose();
            alert = null;
        });
    }
}

Example Usage:

var alert = DependencyService.Get<IDynamicAlert>();
if (alert != null)
{
    alert.Show("StackOverflow", "Starting your request...");
    await Task.Delay(2000); // Do some work...
    alert.Update("Your request is processing...");
    await Task.Delay(2000); // Do some work...
    alert.Update("Your request is complete...");
    await Task.Delay(750);
    alert.Dismiss();
}
else
{
    throw new Exception("IDynamicAlert Dependency not found");
}

Output:

enter image description here

Android Version:

The android version consists of a couple of parts, a DialogFragment subclass and the IDynamicAlert implementation that uses the custom DialogFragment.

Android DialogFragment Subclass:

public class DynamicAlertDialogFragment : DialogFragment
{
    AlertDialog alertDialog;
    readonly Context context;

    public static DynamicAlertDialogFragment Instance(Context context, string title, string message)
    {
        var fragment = new DynamicAlertDialogFragment(context);
        Bundle bundle = new Bundle();
        bundle.PutString("title", title);
        bundle.PutString("message", message);
        fragment.Arguments = bundle;
        return fragment;
    }

    public DynamicAlertDialogFragment(Context context)
    {
        this.context = context;
    }

    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        var title = Arguments.GetString("title");
        var message = Arguments.GetString("message");
        alertDialog = new AlertDialog.Builder(context)
                    .SetIcon(Android.Resource.Drawable.IcDialogInfo)
                    .SetTitle(title)
                    .SetMessage(message)
                    .Create();
        return alertDialog;
    }

    public void SetMessage(string message)
    {
        (context as Activity).RunOnUiThread(() => { alertDialog.SetMessage(message);});
    }
}

Android IDynamicAlert Dependency Implementation:

public class DynamicAlert : IDynamicAlert
{
    const string FRAGMENT_TAG = "DynamicAlert_Fragment";
    DynamicAlertDialogFragment fragment;
    static FormsAppCompatActivity currentActivity;
    public static FormsAppCompatActivity CurrentActivity { set { currentActivity = value; } }

    public void Show(string title, string message)
    {
        if (currentActivity == null) throw new Exception("DynamicAlert.CurrentActivity needs assigned");
        var fragMgr = currentActivity.FragmentManager;
        var fragTransaction = fragMgr.BeginTransaction();
        var previous = fragMgr.FindFragmentByTag(FRAGMENT_TAG);
        if (previous != null)
        {
            fragTransaction.Remove(previous);
        }
        fragTransaction.DisallowAddToBackStack();
        fragment = DynamicAlertDialogFragment.Instance(currentActivity, title, message);
        fragment.Show(fragMgr, FRAGMENT_TAG);
    }

    public void Update(string message)
    {
        if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");
        fragment.SetMessage(message);
    }

    public void Dismiss()
    {
        if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");
        fragment.Dismiss();
        fragment.Dispose();
        fragment = null;
    }
}

Android Init / Usage:

When creating the AlertDialog in the DialogFragment we need access to the current Activity and when using Xamarin.Forms, that is normally the MainActivity that is a FormsAppCompatActivity subclass. Thus you will need to initialize the DynamicAlert.CurrentActivity static property with this Activity in your MainActivity.OnCreate subclass:

Example:

protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;
    base.OnCreate(bundle);

    ////////////
    DynamicAlert.CurrentActivity = this;
    ////////////

    global::Xamarin.Forms.Forms.Init(this, bundle);
    LoadApplication(new App());

}

Android Output:

enter image description here

like image 144
SushiHangover Avatar answered Sep 30 '22 17:09

SushiHangover