应用启动时不再显示消息框
Don't show message box again on app launch
当我的应用程序启动时,会显示一个消息框。如何防止它再次显示,以便当用户点击 "Don't show again" 按钮时,消息框在下次打开时不会启动?
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
}
}
只需向您的自定义消息框添加一个标志。如果用户select "Don't show again",存储标志。接下来你启动并需要显示这个特定的消息框,检查标志。
您必须在某处记录用户选择了该选项。有很多方法可以实现。
一个简单的策略是创建一个代表所有用户选择的对象。当用户做出选择时,更新该对象并将其序列化为例如一份文件。当应用程序首次加载时,从文件中反序列化该对象,以便您可以使用之前的选择。
当用户点击 "Don't show again" 按钮时,更新配置对象以记录选择。
有很多方法可以做到这一点。但是在所有这些选项中,您需要将用户选择存储在某个地方。您可以将其存储在
- 数据库table,将设置与唯一的用户 ID 相关联,例如 LoginID
- 首选项 XML 文件:Refer this
- 作为项目中的设置:Refer this
- 作为注册表项:Refer this
- 一个 INI 文件
当我的应用程序启动时,会显示一个消息框。如何防止它再次显示,以便当用户点击 "Don't show again" 按钮时,消息框在下次打开时不会启动?
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
}
}
只需向您的自定义消息框添加一个标志。如果用户select "Don't show again",存储标志。接下来你启动并需要显示这个特定的消息框,检查标志。
您必须在某处记录用户选择了该选项。有很多方法可以实现。
一个简单的策略是创建一个代表所有用户选择的对象。当用户做出选择时,更新该对象并将其序列化为例如一份文件。当应用程序首次加载时,从文件中反序列化该对象,以便您可以使用之前的选择。
当用户点击 "Don't show again" 按钮时,更新配置对象以记录选择。
有很多方法可以做到这一点。但是在所有这些选项中,您需要将用户选择存储在某个地方。您可以将其存储在
- 数据库table,将设置与唯一的用户 ID 相关联,例如 LoginID
- 首选项 XML 文件:Refer this
- 作为项目中的设置:Refer this
- 作为注册表项:Refer this
- 一个 INI 文件