0

When my app launches a message box is shown. How can I prevent it from being shown again so that when the user taps the "Don't show again" button, the message box doesn't launch the next time it is opened?

            async protected override void OnLaunched(LaunchActivatedEventArgs e)
        {

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();


            MessageDialog md = new MessageDialog("This is a MessageDialog", "Title");
            bool? result = null;
            md.Commands.Add(
               new UICommand("OK", new UICommandInvokedHandler((cmd) => result = true)));
            md.Commands.Add(
               new UICommand("Don't show this again", new UICommandInvokedHandler((cmd) => result = false)));

            await md.ShowAsync();
            if (result == true)
            {
                // do something    
            }
            else if (result == false)
            {
                // do something    
            }

        }
wbk727
  • 8,017
  • 12
  • 61
  • 125

3 Answers3

1

Just add a flag to your custom message box. If the user select "Don't show again", store the flag. Next you launch and need to show this particular message box, check for the flag.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
1

There are many ways to do this. But in all these options, you need to store the users selection somewhere. You can store this in

  1. A database table, associate the setting with a unique user ID, like LoginID
  2. A Preferences XML file : Refer this
  3. As a Setting in your project : Refer this
  4. As a Registry Entry : Refer this
  5. An INI File

You might want to take a look at Persisting Application Settings in the .NET Framework

Community
  • 1
  • 1
Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
0

You have to record somewhere that the user selected that option. There are many ways to accomplish that.

One simple strategy is to create an object that represents all user choices. When the user makes a choice, update that object and serialize it to e.g. a file. When the application first loads, deserialize that object from the file so that previous choices are available to you.

When the user taps the "Don't show again" button, update the configuration object to record the choice.

Eric J.
  • 147,927
  • 63
  • 340
  • 553