UWP - Display Message Dialog 1 time

723 Views Asked by At

I have the code below:

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();
}

I have a problem, that is when I click the OK button, it will display message dialog again. I want message dialog shown only 1 time. How to solve it?

1

There are 1 best solutions below

0
Diado On

The problem is that you are declaring the shown variable in a local scope, so it is being initialised and set every time your show message box code is run.

To avoid this, declare it at a higher level - for instance, at class level. For example, based on the code you shared in the comments:

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
        }
    }
}

This way, the first time you call the method it will check whether the the dialog has been shown, which it won't have been, so it will show the dialog and flag it as having been shown. The next time it will check the flag and not show the dialog.