gpt4 book ai didi

xamarin - DisplayAlert 更改文本 xamarin 表单

转载 作者:行者123 更新时间:2023-12-04 17:16:15 24 4
gpt4 key购买 nike

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

如何做到这一点?

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

我想显示状态...
  • 连接服务器
  • 下载
  • 下载完成
  • 最佳答案

    这是表单和 iOS 的简单“动态警报”使用 UIAlertControllerAndroid使用 DialogFragmentXamarin.Forms依赖服务:

    依赖接口(interface):

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

    输出:

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

    安卓 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;
    }
    }

    安卓初始化/用法:

    创建 AlertDialog 时在 DialogFragment我们需要访问当前的 Activity当使用 Xamarin.Forms ,通常是 MainActivity那是一个 FormsAppCompatActivity子类。因此您需要初始化 DynamicAlert.CurrentActivity带有此 Activity 的静态属性在您的 MainActivity.OnCreate子类:

    例子:
    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

    关于xamarin - DisplayAlert 更改文本 xamarin 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578164/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com