UWP - 显示消息对话框1次

时间:2018-05-03 07:32:05

标签: c# uwp messagedialog

我有以下代码:

if (jumlahiddb < jumlahbuku)
{
        DownloadBukuKomik(url);

        string KomikUpdate = @"INSERT INTO books (id,title,folder_id,identifier) SELECT " + intID + ",'" + namaFile + ".pdf',67,'" + namaFile +
             ".pdf' WHERE not exists (select id AND title AND folder_id AND identifier FROM books WHERE id=" + intID + " and title='" + namaFile +
             ".pdf' AND folder_id=67 and identifier='" + namaFile + ".pdf')";
        Debug.WriteLine(KomikUpdate.ToString());
        var komikQuery = objConnUpdate.Prepare(KomikUpdate);
        komikQuery.Step();
}
else
{
    bool shown = false;
    if (!shown)
    {
        MessageDialog messageDialog1 = new MessageDialog("Jumlah komik bertambah sebanyak " + jumlahbuku + " komik pada menu Komik Pendidikan", "Update Berhasil");
        messageDialog1.Commands.Add(new UICommand("OK", (command) =>
        {
            DownloadBukuVideo.IsOpen = false;
            Downloading.IsOpen = false;
            ukomikBtn.Visibility = Visibility.Visible;
            downloadKomikBtn.Visibility = Visibility.Collapsed;
            ukomikText.Visibility = Visibility.Collapsed;
            ukomikText.Text = "";
            shown = true;
        }));
         await messageDialog1.ShowAsync();
}

我遇到了问题,就是当我点击“确定”按钮时,它会再次显示消息对话框。我想只显示一次消息对话框。怎么解决?

1 个答案:

答案 0 :(得分:0)

问题是您在本地范围内声明shown变量,因此每次运行显示消息框代码时都会初始化并设置

要避免这种情况,请将其声明为更高级别 - 例如,在类级别。例如,根据您在评论中分享的代码:

class myClass {

    private bool _shown;

    public async void KomikMsgDialog()
    {
        if (!_shown) // If we haven't shown the dialog yet
        {
            MessageDialog messageDialog1 = new MessageDialog("Jumlah komik bertambah sebanyak " + jumlahbuku + " komik pada menu Komik Pendidikan", "Update Berhasil");
            messageDialog1.Commands.Add(new UICommand("OK", (command) =>
            {
                DownloadBukuVideo.IsOpen = false;
                Downloading.IsOpen = false;
                ukomikBtn.Visibility = Visibility.Visible;
                downloadKomikBtn.Visibility = Visibility.Collapsed;
                ukomikText.Visibility = Visibility.Collapsed;
                ukomikText.Text = "";
            }));
            await messageDialog1.ShowAsync();
            _shown = true; // Flag the dialog as having been shown
        }
    }
}

这样,第一次调用方法时,它将检查对话框是否已经显示,它将不会显示,因此它将显示对话框并将其标记为已显示。下次它将检查标志而不显示对话框。