Xamarin在唤醒方法时加载消息

时间:2017-08-01 15:59:36

标签: c# android multithreading xamarin

我正在尝试生成一条加载消息,该消息将在完成两个方法调用时运行。方法为SetDataFindNotesPerDay

尝试此操作时我一直在使用进度对话框但是我没有运气,进度对话框没有打开或出现但是没有加载方法或消失。

下面是代码是我试图尝试加载对话框的代码。

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Create your application here
    SetContentView(Resource.Layout.TimeTable);

    expandableListView = FindViewById<ExpandableListView>(Resource.Id.ListViewExpanded);
    StartMonday = FindViewById<TextView>(Resource.Id.TextViewStartDateMonday);
    SelectDateButton = FindViewById<Button>(Resource.Id.SelectDateButton);
    NotesListView = FindViewById<ListView>(Resource.Id.NotesListView);

    // Populate User 
    User = JsonConvert.DeserializeObject<UserInstance>(Intent.GetStringExtra("User"));

    //Find Monday 
    DateTime TodaysDate = DateTime.Now;
    while (TodaysDate.DayOfWeek != DayOfWeek.Monday) TodaysDate = TodaysDate.AddDays(-1);

    StartMonday.Text = "Starting On " + TodaysDate.ToLongDateString();

    // Loading Message While These Load!!!!!!!!!!!!!!!!!!
    SetData(TodaysDate, out myAdapater);
    FindNotesForDay(TodaysDate, TodaysDate.AddDays(+6));

    SelectDateButton.Click += delegate {
        DateSelectOnClick();
    };
}
下面的

是我尝试显示加载消息

ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);

progress.SetMessage("Contacting server. Please wait...");
progress.SetCancelable(true);
progress.Show();


var progressDialog = ProgressDialog.Show(this, "Please wait...", "Checking account info...", true);

//Find Monday 
DateTime TodaysDate = DateTime.Now;
while (TodaysDate.DayOfWeek != DayOfWeek.Monday) TodaysDate = TodaysDate.AddDays(-1);

new Thread(new ThreadStart(delegate
{
    RunOnUiThread(() => Toast.MakeText(this, "Toast within progress dialog.", ToastLength.Short).Show());
    RunOnUiThread(() => SetData(TodaysDate, out myAdapater));
    RunOnUiThread(() => FindNotesForDay(TodaysDate, TodaysDate.AddDays(+6)));
    RunOnUiThread(() => progressDialog.Hide());

})).Start()

2 个答案:

答案 0 :(得分:1)

  

尝试此操作时我一直在使用进度对话框但是我没有运气,进度对话框没有打开或出现但是没有加载方法或消失。

您可以为此作品创建AsyncTask,例如:

private class MyTask : AsyncTask
{
    private ProgressDialog progress;
    private Context context;

    public MyTask(Context mcontext)
    {
        context = mcontext;
    }

    protected override void OnPreExecute()
    {
        base.OnPreExecute();
        progress = new ProgressDialog(context);
        progress.Indeterminate = true;
        progress.SetProgressStyle(ProgressDialogStyle.Spinner);

        progress.SetMessage("Contacting server. Please wait...");
        progress.SetCancelable(true);
        progress.Show();
    }

    protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
    {
        //do your work here and return the result
        PublishProgress(10);
        return null;
    }

    protected override void OnProgressUpdate(params Java.Lang.Object[] values)
    {
        base.OnProgressUpdate(values);
        Task.Delay(2000).ContinueWith(t =>
        {
            progress.SetMessage("Checking account info...");
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }

    protected override void OnPostExecute(Java.Lang.Object result)
    {
        base.OnPostExecute(result);
        Task.Delay(5000).ContinueWith(t =>
        {
            progress.Dismiss();
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }
}

您可以像这样调用此任务:

var task = new MyTask(this);
task.Execute();

这只是一个示例,您应该能够将任务延迟部分替换为您的逻辑代码。

答案 1 :(得分:0)

这是我的解决方案,但我必须说旋转器目前还没有工作,但对话框确实

谢谢, 乔

namespace PleaseWorkV1
{
[Activity(Label = "TimetableMenu")]
public class TimetableMenu : Activity
{
    public Button ClassTimetableButton;
    public Button PlacementTimetableButton;
    public Button DownloadCalBTN;
    public UserInstance User;
    public GetConnectionClass Connect = new GetConnectionClass();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.TimeTableMenu);

        ClassTimetableButton = FindViewById<Button>(Resource.Id.ClassTimeTableBTN);
        PlacementTimetableButton = FindViewById<Button>(Resource.Id.PlacementTimeTableBTN);
        DownloadCalBTN = FindViewById<Button>(Resource.Id.DownloadMonthBTN);



        User = JsonConvert.DeserializeObject<UserInstance>(Intent.GetStringExtra("User"));

        ClassTimetableButton.Click += async (sender, args) => await ClassOnClick();

        //ClassTimetableButton.Click += delegate
        //{
        //    ClassOnClick();
        //};

        PlacementTimetableButton.Click += delegate
        {
            PlacementOnClick();
        };

        DownloadCalBTN.Click += delegate
        {
            DownloadCalBTNOnClick();
        };
    }


    private async Task ClassOnClick()
    {
        var progressDialog = ProgressDialog.Show(this, "Loading", "Message", false);

        var MoveToTimeTable = new Intent(this, typeof(TimeTable));
        MoveToTimeTable.PutExtra("User", JsonConvert.SerializeObject(User));
        StartActivity(MoveToTimeTable);

        await Task.Delay(0)
            .ContinueWith(task => { StartActivity(MoveToTimeTable); });

        progressDialog.Hide();

    }