在完成任务之前如何显示进度指示器?

时间:2014-03-20 13:17:41

标签: c# windows-phone-8

我的应用中遇到了一个问题。在此部分中,在执行此方法之前不会显示进度指示器。

if (!SQLiteFunctions.IsResultCriteriaInitialized())
{
     SystemFunctions.InitializeResultCriterias();
}

此方法仅在用户使用此应用时首次执行。这需要几秒钟,因为此方法必须初始化SQLite数据库中的条件以评估结果。但是,在此方法之前,我将进度指示器设置为true,但在上述方法完成之前它不会显示,其余部分已完成。

所以,我的问题是如何在进行上述方法之前显示进度指示器。

这是我的代码

private void submit_button_Click(object sender, RoutedEventArgs e)
    {
        if (String.IsNullOrWhiteSpace(temWeight_textBox.Text) || String.IsNullOrWhiteSpace(temHeight_textBox.Text) ||
            String.IsNullOrWhiteSpace(temAge_textBox.Text))
        {
            MessageBox.Show("Weight, height or age cannot be empty", AppResources.Warning, MessageBoxButton.OK);
        }
        else
        {
            SystemFunctions.SetProgressIndicatorProperties(true);
            SystemTray.ProgressIndicator.Text = "Analyzing...";

            double userWeight = Convert.ToDouble(temWeight_textBox.Text);
            double userHeight = Convert.ToDouble(temHeight_textBox.Text);
            int userAge = Convert.ToInt32(temAge_textBox.Text);
            string userGender = temGender_picker.SelectedItem.ToString();
            Boolean userHealthInfo1 = (Boolean)temHealthInfo_checkBox1.IsChecked;
            Boolean userHealthInfo2 = (Boolean)temHealthInfo_checkBox2.IsChecked;
            Boolean userHealthInfo3 = (Boolean)temHealthInfo_checkBox3.IsChecked;
            Boolean userHealthInfo4 = (Boolean)temHealthInfo_checkBox4.IsChecked;
            Boolean userHealthInfo5 = userGender == "Female" ? (Boolean)temHealthInfo_checkBox5.IsChecked : false;

            if (!SQLiteFunctions.IsResultCriteriaInitialized())
            {
                SystemFunctions.InitializeResultCriterias();
            }
            var result = new FecesAnalyzer().analyzeData(poopColor, poopShape, painLevel, bloodAmount, userWeight, userHeight, userGender, userAge,
                userHealthInfo1, userHealthInfo2, userHealthInfo3, userHealthInfo4, userHealthInfo5, isMelena, havingMedicines);

            List<ResultAndRecommendationDictionary> serializedResult = SystemFunctions.SerializeUserResultAndRecommendationData(result);
            SessionManagement.StoreUserLastestResultsAndRecommendation(serializedResult);
            SystemFunctions.SetProgressIndicatorProperties(false);

            List<string> necessaryInfo = result["NecessaryInfo"];
            Boolean isAdditionalAskingNeeded = Convert.ToBoolean(necessaryInfo[0]);
            if (isAdditionalAskingNeeded == false)
                NavigationService.Navigate(new Uri("/ResultPage.xaml?poopColor=" + poopColor + "&shape=" + poopShape + "&painLevel=" + painLevel
                + "&bloodAmount=" + bloodAmount + "&melenaResult=" + isMelena + "&havingMedicines=" + havingMedicines
                + "&userPoopStoredDateTime=" + userPoopDateTime, UriKind.Relative));
            else
                NavigationService.Navigate(new Uri("/AdditionalHealthInfomation2.xaml?poopColor=" + poopColor + "&shape=" + poopShape + "&painLevel=" + painLevel
                + "&bloodAmount=" + bloodAmount + "&melenaResult=" + isMelena + "&havingMedicines=" + havingMedicines
                + "&userPoopStoredDateTime=" + userPoopDateTime, UriKind.Relative));
        }
    }

InitializeResultCriterias()方法的示例代码

public static void InitializeResultCriterias()
    {
        // Instantiate SQLite Function
        SQLiteFunctions db_function = new SQLiteFunctions();

        // ----- Color_Meaning_Table_SQLite : Very light brown -----                   
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Very light brown", Meaning = "Lacks in fiber" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Very light brown", Meaning = "Possibly take too much fat diet" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Very light brown", Meaning = "Possible sign of liver problems or constipation" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Very light brown", Meaning = "Possible sign of diarrhea" });

        // ----- Color_Meaning_Table_SQLite : Medium brown -----
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Medium brown", Meaning = "Normal stool" });

        // ----- Color_Meaning_Table_SQLite : Black -----
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possibly eat dark colored foods" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possibly take too much iron" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possibly drink too much alcohol" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of gastric or duodenal ulcers" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of bleeding esophageal varices" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible other types of bleeding in GI tract" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possibly take certain medications" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of abdominal pain" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of vomiting" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of diarrhea" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of weakness" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Black", Meaning = "Possible sign of dizziness" });

        // ----- Color_Meaning_Table_SQLite : Maroon -----
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Maroon", Meaning = "Possible sign of intestinal parasites or infection" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Maroon", Meaning = "Possible sign of diverticulitis" });
        db_function.InsertData(new Color_Meaning_Table_SQLite() { Name = "Maroon", Meaning = "Possible sign of irritable bowel syndrome(IBS)" });
    ........
   }

谢谢

1 个答案:

答案 0 :(得分:1)

您的代码阻止了UI,执行异步方法:

if (!SQLiteFunctions.IsResultCriteriaInitialized())
{
    await Task.Run(() => SystemFunctions.InitializeResultCriterias());
}

要在您的方法中使用await,需要使用async关键字标记,但您已在代码中使用该关键字,因此您可以继续使用。