DisplayAlert 随着更改文本 xamarin 表单

2023-11-24

我有一个要求,我必须在 DisplayAlert 上显示下载状态。但是可以异步更改文本。

如何实现这一目标?

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

我想显示状态,例如...

  • 已连接到服务器
  • 正在下载
  • 下载完成

这是一个简单的表单“动态警报”iOS using UIAlertController and Android用一个DialogFragment and a Xamarin.Forms依赖服务:

依赖接口:

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

iOS IDynamicAlert依赖实现:

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

用法示例:

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 版本由几个部分组成,DialogFragment子类和IDynamicAlert使用自定义的实现DialogFragment.

Android DialogFragment 子类:

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依赖实现:

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 初始化/使用:

创建时AlertDialog in the DialogFragment我们需要访问当前的Activity以及使用时Xamarin.Forms,这通常是MainActivity这是一个FormsAppCompatActivity子类。因此您需要初始化DynamicAlert.CurrentActivity静态属性与此Activity在你的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());

}

安卓输出:

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DisplayAlert 随着更改文本 xamarin 表单 的相关文章

随机推荐