用户登录时以编程方式显示进度条

问题描述 投票:0回答:1

我在Centering ProgressBar Programmatically in Android找到了这个类,它会以编程方式显示一个进度条,问题是它是一个Xamarin Android Studio示例,我正在尝试将它转换为Xamarin for Visual Studio 2017.这是我用这些行成功转换的代码似乎找不到Xamarin VS 2017的等价物。

using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace OML_Android
{
    class ProgressBarHandler
    {
        private ProgressBar mProgressBar;
        private Context mContext;

        public ProgressBarHandler(Context context)
        {
            mContext = context;

            ViewGroup layout = (ViewGroup)((Activity)context).FindViewById(Android.Resource.Id.Content).RootView;

            mProgressBar = new ProgressBar(context, null, Android.Resource.Attribute.ProgressBarStyleLarge);

            // there is no setIndeterminate method for progressbar
            mProgressBar.setIndeterminate(true);

            // I cannot find an equivilent for LayoutParams
            RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);

            RelativeLayout rl = new RelativeLayout(context);

            // No equivalent for Gravity.CENTER
            rl.SetGravity(Gravity.CENTER);
            rl.AddView(mProgressBar);

            layout.AddView(rl, params);

            hide();
        }

        public void show()
        {
            mProgressBar.Visibility = Android.Views.ViewStates.Visible;
        }

        public void hide()
        {
            mProgressBar.Visibility = Android.Views.ViewStates.Invisible;
        }
    }
}

一旦我转换和工作,我希望它覆盖我的登录视图,直到视图完成处理。

xamarin.android visual-studio-2017
1个回答
0
投票

我帮你将Java代码转换为C#,运行GIF。

enter image description here

有代码。

    class ProgressBarHandler
{
    private ProgressBar mProgressBar;
    private Context mContext;

    public ProgressBarHandler(Context context)
    {
        mContext = context;

        ViewGroup layout = (ViewGroup)((Activity)context).FindViewById(Android.Resource.Id.Content).RootView;

        mProgressBar = new ProgressBar(context, null, Android.Resource.Attribute.ProgressBarStyleLarge);

        // there is no setIndeterminate method for progressbar
      //  mProgressBar.SetIndeterminate(true);
        mProgressBar.Indeterminate = true;
        // I cannot find an equivilent for LayoutParams
        RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);

        RelativeLayout rl = new RelativeLayout(context);

        // No equivalent for Gravity.CENTER
        rl.SetGravity(GravityFlags.Center   );
        rl.AddView(mProgressBar);

        layout.AddView(rl, layoutparams);

        hide();
    }

    public void show()
    {
        mProgressBar.Visibility = Android.Views.ViewStates.Visible;
    }

    public void hide()
    {
        mProgressBar.Visibility = Android.Views.ViewStates.Invisible;
    }
}

您可以直接使用它,如下面的Activity中的代码。

var progress= new ProgressBarHandler(this);
progress.show();
© www.soinside.com 2019 - 2024. All rights reserved.