DisplayAlert改变文本xamarin表单

时间:2017-04-24 00:50:53

标签: xamarin xamarin.ios xamarin.forms

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

如何实现这一目标?

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

我希望显示状态......

  • 已连接至服务器
  • 下载
  • 下载完成

1 个答案:

答案 0 :(得分:9)

以下是使用iOSUIAlertController依赖服务使用AndroidDialogFragment的表单和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");
}

输出:

enter image description here

Android版:

Android版本包含几个部分,DialogFragment子类和使用自定义IDynamicAlert的{​​{1}}实现。

Android DialogFragment子类:

DialogFragment

Android 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

Android初始化/用法:

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

}

Android输出:

enter image description here