当我的应用程序启动时,会显示一个消息框。如何防止它再次显示,以便当用户点击“不再显示”按钮时,消息框在下次打开时不会启动?
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
}
}
答案 0 :(得分:1)
只需在自定义消息框中添加标记即可。如果用户选择"不再显示",请存储该标志。接下来你启动并需要显示这个特定的消息框,检查标志。
答案 1 :(得分:1)
有很多方法可以做到这一点。但是在所有这些选项中,您需要在某处存储用户选择。您可以将其存储在
中您可能需要查看Persisting Application Settings in the .NET Framework
答案 2 :(得分:0)
您必须记录用户选择该选项的位置。有很多方法可以实现这一目标。
一个简单的策略是创建一个代表所有用户选择的对象。当用户做出选择时,更新该对象并将其序列化为例如一份文件。首次加载应用程序时,从文件中反序列化该对象,以便您可以使用以前的选项。
当用户点击“再次显示”时,"按钮,更新配置对象以记录选择。