DisplayAlert With text reshaping xamarin

I have a requirement for when should I show the loading status on the DisplayAlert. But changing the text on it is asynchronous.

How to do it?

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

      

I want to show the status as ...

  • Connected to server
  • Loading
  • loading is complete
+3


source to share


1 answer


Here is a simple "Dynamic Signal" for forms iOS

using UIAlertController

both Android

Dependency Service DialogFragment

and Xamarin.Forms

:

Dependency interface:

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

      

iOS IDynamicAlert

Implementation of dependencies:

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;
        });
    }
}

      

Usage example:

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 subclass DialogFragment

and an implementation IDynamicAlert

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 Execution:

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 created AlertDialog

in, DialogFragment

we need access to the current one Activity

and when used Xamarin.Forms

, usually this MainActivity

, which is a subclass FormsAppCompatActivity

. Thus, you will need to initialize a static property DynamicAlert.CurrentActivity

with this Activity

in your subclass MainActivity.OnCreate

:

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

+7


source







All Articles