如何将进度条与Xamarin中的按钮与Visual Studio相关联?

时间:2016-05-05 09:50:21

标签: c# android xamarin

我是Xamarin世界的新手,所以我真正需要你的帮助。在我的第一个应用程序中,我想将进度条与按钮相关联。因此,当我单击按钮时,进度条会启动,当进度结束时,进度条会消失。

这是我的C#代码:

translateButton.Click += (object sender, EventArgs e) =>
{
    ProgressBar pb = FindViewById<ProgressBar>(Resource.Id.progressBar1);
    Thread.Sleep(1000);
    pb.Enabled = true;

    translateNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
    if (string.IsNullOrWhiteSpace(translateNumber))
    {
         callButton.Text = "Call";
         callButton.Enabled = false;
         pb.Enabled = true;
    }

    else
    {
        callButton.Text = "Call" + translateNumber;
        callButton.Enabled = true;
        pb.Enabled = true;
    }
};

此代码来自Xamarin网站上的tutorial

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

ProgressBar pb;
Button translateButton;
EditText phoneNumberText;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Main);

    translateButton = FindViewById<Button>(Resource.Id.translateButton);
    phoneNumberText = FindViewById<EditText>(Resource.Id.phoneNumberText);
    pb = FindViewById<ProgressBar>(Resource.Id.pb);
    pb.Visibility = ViewStates.Invisible;

    translateButton.Click += translateButtonClicked;
}

async void translateButtonClicked (object sender, EventArgs e)
{
    pb.Visibility = ViewStates.Visible;
    await myMethod();
    pb.Visibility = ViewStates.Gone;
}

async Task myMethod()
{
    await Task.Run(() => {
        //
        // Do the work stuff here
        //
    });
}