xamarin - DisplayAlert 更改文本 xamarin 表单

标签 xamarin xamarin.ios xamarin.forms

我有一个要求,我必须在 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/

    相关文章:

    android - 使用 native 库功能的 Xamarin 表单应用程序

    xamarin - ViewModel 中使用ConfigureAwait(false) 何时会出现问题?

    ios - Xamarin iOS Mac 构建主机端口号

    objective-c - 如何使用 Objective Sharpie 绑定(bind) Objective-C 库中的 C 数组类型?

    c# - Xamarin.iOS ARKit 演示项目错误 : “32-bit architectures are not supported when deployment target is 11 or later"

    c# - 在 MVVMCross 中将对象传递到 "navigated to" View 模型的最佳方法是什么?

    c# - 如何从 Xamarin.Forms 应用程序修改平台特定属性?

    c# - Xamarin.Essentials "The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class."

    c# - Xamarin.Forms - Webview(带有本地文件)在 Windows Phone 8.1 和 10 上不工作

    ios - Xamarin.iOS coreML 在后台模式下获取预测空引用错误