DisplayAlert 更改文本 xamarin 形式 [英] DisplayAlert With changing Text xamarin forms

查看:25
本文介绍了DisplayAlert 更改文本 xamarin 形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何实现这一目标?

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

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

I want to show status like...

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

推荐答案

这里是一个简单的动态警报",适用于 Forms 和 iOS,使用 UIAlertControllerAndroid 使用 DialogFragmentXamarin.Forms 依赖服务:

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

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

iOS IDynamicAlert 依赖实现:

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

示例用法:

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

输出:

android 版本由几个部分组成,一个 DialogFragment 子类和使用自定义 DialogFragmentIDynamicAlert 实现.

The android version consists of a couple of parts, a DialogFragment subclass and the IDynamicAlert implementation that uses the custom 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 依赖实现:

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

DialogFragment 中创建 AlertDialog 时,我们需要访问当前的 Activity 以及使用 Xamarin.Forms 时>,通常是 MainActivity,它是 FormsAppCompatActivity 的子类.因此,您需要使用 MainActivity.OnCreate 子类中的此 Activity 初始化 DynamicAlert.CurrentActivity 静态属性:

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:

示例:

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

}

这篇关于DisplayAlert 更改文本 xamarin 形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆