Xamarin.android 如何在xamarin android中创建进度微调器

Xamarin.android 如何在xamarin android中创建进度微调器,xamarin.android,Xamarin.android,我需要一个教程或代码来在xamarin android中创建进度微调器,就像照片中一样。您有xamarin的ProgressDialog控件。android允许您显示带有微调器的对话框: 否则,如果要在布局上使用微调器,请执行以下操作: 如果我理解正确,您需要创建一个ProgressBar,并将不确定属性设置为true,您可以在 但正如官方文件所说: ProgressDialog是一个模式对话框,它阻止用户与应用程序交互。不要使用这个类,你应该使用一个进度指示器,比如,它可以嵌入到你的应用程序的

我需要一个教程或代码来在xamarin android中创建进度微调器,就像照片中一样。

您有xamarin的
ProgressDialog
控件。android允许您显示带有微调器的对话框:

否则,如果要在布局上使用微调器,请执行以下操作:


如果我理解正确,您需要创建一个
ProgressBar
,并将
不确定属性设置为
true

,您可以在

但正如官方文件所说:

ProgressDialog是一个模式对话框,它阻止用户与应用程序交互。不要使用这个类,你应该使用一个进度指示器,比如,它可以嵌入到你的应用程序的UI中。或者,您可以使用通知通知用户任务的进度


您可以在

ProgressDialog上查看一个很棒的教程,它已被弃用,所以请使用ProgressBar

AXML代码:在资源布局中创建my_custom_progress_bar.AXML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="16dp">
 <TableRow
   android:layout_centerInParent="true"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
   <ProgressBar
       android:id="@+id/progress_bar"
       android:layout_width="wrap_content"
       android:layout_height="match_parent" />
    <TextView
       android:gravity="center|left"
       android:id="@+id/tv_message"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginLeft="16dp" />
  </TableRow>
</RelativeLayout>

Xamarin Android代码:在活动中使用以下方法,并在需要时使用。如果希望在整个应用程序中使用,我们可以将其保留在utils类中

private Android.App.AlertDialog _progressBarDialog;

public void ShowProgressBar(string message)
    {
        Android.App.AlertDialog.Builder dialogBuilder = new Android.App.AlertDialog.Builder(this);
        var inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
        var dialogView = inflater.Inflate(Resource.Layout.my_custom_progress_bar, null);
        dialogBuilder.SetView(dialogView);
        dialogBuilder.SetCancelable(false);
        var tvMsg = dialogView.FindViewById<TextView>(Resource.Id.tv_msg);
        tvMsg.Text = message;
        _progressBarDialog= dialogBuilder.Create();
        _progressBarDialog.Show();
    }


public void HideProgressBar()
    {
        if (_progressBarDialog!= null)
        {
            _progressBarDialog.Dismiss();
        }
    }
private Android.App.AlertDialog\u progressBarDialog;
公共void ShowProgressBar(字符串消息)
{
Android.App.AlertDialog.Builder dialogBuilder=新的Android.App.AlertDialog.Builder(此);
var充气器=(LayoutInflater)GetSystemService(Context.LayoutInflaterService);
var dialogView=inflater.Inflate(Resource.Layout.my_自定义_进度_栏,null);
dialogBuilder.SetView(dialogView);
dialogBuilder.SetCancelable(false);
var tvMsg=dialogView.findviewbyd(Resource.Id.tv_msg);
Text=消息;
_progressBarDialog=dialogBuilder.Create();
_progressBarDialog.Show();
}
public void HideProgressBar()
{
如果(_progressBarDialog!=null)
{
_progressBarDialog.disclose();
}
}
这正在工作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;

namespace Android.Basic.UI
{
    
    public class ProgressBarDialog
    {
        private TextView tvText { get; }
        private AlertDialog dialog;
        private LinearLayout ll;
        private ProgressBar progressBar;
        TextView valTxt;
        TextView maxTxt;
        AlertDialog.Builder builder;
        public ProgressBarDialog(Context context)
        {
            int llPadding = 30;
            ll = new LinearLayout(context);
            ll.Orientation = Orientation.Vertical;
            ll.SetPadding(llPadding, llPadding, llPadding, llPadding);
            ll.SetGravity(GravityFlags.Center);
            LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WrapContent,
                    LinearLayout.LayoutParams.WrapContent);
            //Add Header
            LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
                   LinearLayout.LayoutParams.WrapContent,
                   LinearLayout.LayoutParams.WrapContent);
            tvParam.Gravity = GravityFlags.Left;
            tvText = new TextView(context);
            tvText.Text = ("Loading ...");
            tvText.SetTextColor(Color.Black);
            tvText.SetTextSize(Android.Util.ComplexUnitType.Dip, 18);
            tvText.LayoutParameters = (tvParam);
            ll.AddView(tvText);
            //Add Progress
            LinearLayout.LayoutParams progressBarParam = new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.MatchParent,
                  LinearLayout.LayoutParams.WrapContent);
            progressBar = new ProgressBar(context, null, Android.Resource.Attribute.ProgressBarStyleHorizontal);
            progressBar.Indeterminate = true;
            progressBar.SetPadding(0, 0, 0, 0);
            progressBar.LayoutParameters = (progressBarParam);
            ll.AddView(progressBar);
            //Progress Labels
            RelativeLayout rl = new RelativeLayout(context);
            var rlParam = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.MatchParent,
                    LinearLayout.LayoutParams.WrapContent);
            rl.LayoutParameters = rlParam;
            valTxt = new TextView(context);
            valTxt.Text = "25";
            valTxt.SetTextColor(Color.White);
            valTxt.LayoutParameters = tvParam;

            var maxParam = new RelativeLayout.LayoutParams(
                   LinearLayout.LayoutParams.WrapContent,
                   LinearLayout.LayoutParams.WrapContent);
            maxParam.AddRule(LayoutRules.AlignParentRight);
            maxTxt = new TextView(context);
            maxTxt.LayoutParameters = maxParam;
            maxTxt.Text = "100";
            maxTxt.SetTextColor(Color.White);

            rl.AddView(valTxt);
            rl.AddView(maxTxt);
            ll.AddView(rl);


            llParam.Gravity = GravityFlags.Center;
            ll.LayoutParameters = (llParam);
            builder = new AlertDialog.Builder(context);
            builder.SetCancelable(false);
            
            builder.SetView(ll);

            dialog = builder.Create();
            dialog.SetCanceledOnTouchOutside(false);
            dialog.CancelEvent += Dialog_CancelEvent;
            Window window = dialog.Window;
            if (window != null)
            {
                var windowManager = context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();// new WindowManager.LayoutParams();

                var layoutParams = new WindowManagerLayoutParams(
                ViewGroup.LayoutParams.MatchParent,
                ViewGroup.LayoutParams.MatchParent,
                WindowManagerTypes.Phone,
                WindowManagerFlags.Fullscreen,
                Format.Translucent);
                layoutParams.CopyFrom(dialog.Window.Attributes);
                layoutParams.Width = LinearLayout.LayoutParams.WrapContent;
                layoutParams.Height = LinearLayout.LayoutParams.WrapContent;
                dialog.Window.Attributes = (layoutParams);
            }

        }
        public event EventHandler Cancelled;
        private void Dialog_CancelEvent(object sender, EventArgs e)
        {
            Cancelled?.Invoke(this, null);
        }
        public Color ProgressColor
        {
            set
            {
                progressBar.IndeterminateDrawable.SetColorFilter(value, Android.Graphics.PorterDuff.Mode.Multiply);
            }
        }
        public void SetProgress(int progress)
        {
            progressBar.SetProgress(progress, true);
        }
        public ViewStates ValueStates
        {
            set
            {
                maxTxt.Visibility = value;
                valTxt.Visibility = value;
            }
        }
        public string Title
        {
            get { return tvText.Text; }
            set
            {
                Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                {
                    tvText.Text = value;
                });
               
            }
        }
        public string ValueLeft
        {
            get { return valTxt.Text; }
            set
            {
                Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                {
                    valTxt.Text = value;
                });
            }
        }
        public string ValueRight
        {
            get { return maxTxt.Text; }
            set
            {
                Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                {
                    maxTxt.Text = value;
                });
               
            }
        }
        public Color TextColor
        {
            set
            {
                tvText.SetTextColor(value);
                valTxt.SetTextColor(value);
                maxTxt.SetTextColor(value);
            }
        }
        public bool Indeterminate
        {
            get { return progressBar.Indeterminate; }
            set
            {
                progressBar.Indeterminate = value;
            }
        }
        public bool Cancelable
        {
            set
            {
                dialog.SetCancelable(value);
                dialog.SetCanceledOnTouchOutside(value);
                builder.SetCancelable(value);
            }
        }
        public int Maximum
        {
            get { return progressBar.Max; }
            set
            {
                progressBar.Max = value;
            }
        }
        public void SetBackgroundDrawableResource(int resId)
        {
            ll.SetBackgroundResource(resId);            
        }
        public void Show()
        {
            dialog.Show();
        }
        public void Hide(bool setNull = true)
        {
            if (dialog != null)
            {
                dialog.Hide();
                if (setNull)
                {
                    dialog = null;
                }
            }
        }
    }
    public class ProgressDrawable : Drawable
    {
        private const int NUM_RECTS = 10;
        Paint mPaint = new Paint();
        protected override bool OnLevelChange(int level)
        {
            this.InvalidateSelf();
            return true;
        }
        public override int Opacity => (int)Format.Translucent;

        public override void Draw(Canvas canvas)
        {
            int level = Level;
            Rect b = Bounds;
            float width = b.Width();
            for (int i = 0; i < NUM_RECTS; i++)
            {
                float left = width * i / NUM_RECTS;
                float right = left + 0.9f * width / NUM_RECTS;
                mPaint.Color = ((i + 1) * 10000 / NUM_RECTS <= level ? Color.Black : Color.Gray);
                canvas.DrawRect(left, b.Top, right, b.Bottom, mPaint);
            }
        }

        public override void SetAlpha(int alpha)
        {
            
        }

        public override void SetColorFilter(ColorFilter colorFilter)
        {
            
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.Graphics;
使用Android.Graphics.Drawables;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Java.Lang;
名称空间Android.Basic.UI
{
公共课进度对话
{
私有文本视图tvText{get;}
私人警报对话框;
私人线路布局;
私人ProgressBar ProgressBar;
文本视图;
文本视图maxTxt;
AlertDialog.Builder;
公共进程对话(上下文)
{
int=30;
ll=新的线性布局(上下文);
ll.方向=方向垂直;
ll.SetPadding(llPadding,llPadding,llPadding,llPadding);
ll.设置重力(重力梯度中心);
LinearLayout.LayoutParams llParam=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WrapContent,
LinearLayout.LayoutParams.WrapContent);
//添加标题
LinearLayout.LayoutParams tvParam=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WrapContent,
LinearLayout.LayoutParams.WrapContent);
tvParam.Gravity=GravityFlags.Left;
tvText=新文本视图(上下文);
Text=(“加载…”);
tvText.SetTextColor(Color.Black);
setextsize(Android.Util.ComplexUnitType.Dip,18);
tvText.LayoutParameters=(tvParam);
ll.AddView(tvText);
//添加进度
LinearLayout.LayoutParams progressBarParam=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MatchParent,
LinearLayout.LayoutParams.WrapContent);
progressBar=新的progressBar(上下文,null,Android.Resource.Attribute.ProgressBarStyleHorizontal);
progressBar.Undeterminate=true;
设置填充(0,0,0,0);
progressBar.LayoutParameters=(progressBarParam);
ll.AddView(progressBar);
//进度标签
RelativeLayout rl=新的RelativeLayout(上下文);
var rlParam=新的RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.MatchParent,
LinearLayout.LayoutParams.WrapContent);
rl.LayoutParameters=rlParam;
valTxt=新文本视图(上下文);
valTxt.Text=“25”;
valTxt.SetTextColor(Color.White);
valTxt.LayoutParameters=tvParam;
var maxParam=新的RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.WrapContent,
LinearLayout.LayoutParams.WrapContent);
maxParam.AddRule(LayoutRules.AlignParentRight);
maxTxt=新文本视图(上下文);
maxTxt.LayoutParameters=maxParam;
maxTxt.Text=“100”;
maxTxt.SetTextColor(Color.White);
rl.AddView(valTxt);
rl.AddView(maxTxt);
ll.AddView(rl);
llParam.重力=重力梯度中心;
ll.LayoutParameters=(llParam);
builder=新建AlertDialog.builder(上下文);
builder.SetCancelable(false);
builder.SetView(ll);
dialog=builder.Create();
对话框。SetCanceledOnTouchOutside(false);
dialog.CancelEvent+=dialog\u CancelEvent;
Window=dialog.Window;
如果(窗口!=null)
{
var windowManager=context.GetSystemService(context.WindowService.JavaCast();//新建windowManager.LayoutParams();
var layoutParams=新的WindowManagerLayoutParams(
ViewGroup.LayoutParams.MatchParent,