[英]How to Show a Page if Application is Launched for the First Time
I am wondering how to signal whether an appication is launched for the very first time, or has already been launched before. 我想知道如何发出信号是首次启动,还是之前已经启动过。 The reason I want to do this is to show a very short informative message before the application is ever used, while every other time the application is launched nothing shows. 我要执行此操作的原因是,在使用该应用程序之前显示一条非常简短的信息,而每隔两次启动该应用程序就不会显示任何信息。 Would I place something in App.xaml.cs like the following 我可以在App.xaml.cs中放置以下内容吗
var settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("WasLaunched"))
{
MessageBox.Show("First time to launch");
settings.Add("WasLaunched", true);
}
And if (!settings.Contains("WasLaunched")
navigate to the 'first launch page' as opposed to the 'main page'? Can someone point me to any good references on this implementation? 如果(!settings.Contains("WasLaunched")
导航到“第一启动页面”而不是“主页”,有人可以指出我关于此实现的任何好的参考吗?
EDIT** 编辑**
I changed my WMAppManifest.xml
default page to LaunchPage.xaml
我将WMAppManifest.xml
默认页面更改为LaunchPage.xaml
<DefaultTask Name="_default" NavigationPage="LaunchPage.xaml" />
And created my UriMapper class 并创建了我的UriMapper类
public class LoginUriMapper : UriMapperBase
{
public override Uri MapUri(Uri uri)
{
if (uri.OriginalString == "/LaunchPage.xaml")
{
if (Settings.FirstLoad.Value == true)
{
//Navigate to Welcome Page with quick first time user info
uri = new Uri("/Views/WelcomePage.xaml", UriKind.Relative);
}
else
{
///Navigate to the actual Main Page
uri = new Uri("/MainPage.xaml", UriKind.Relative);
}
}
return uri;
}
}
But how do I change App.xaml.cs accordingly 但是我如何相应地更改App.xaml.cs
private void Application_Launching(object sender, LaunchingEventArgs e)
{
//how to check and navigate to correct page for this specific method?
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
//how to check and navigate to correct page for this specific method?
}
You'd better to use the power of UriMapper 您最好使用UriMapper的功能
Here you can find a good article . 在这里您可以找到一篇不错的文章 。
The core idea is: 核心思想是:
You should define an empty page ( EntryPage.xaml
) and set it as a default page of your app. 您应该定义一个空白页面( EntryPage.xaml
)并将其设置为应用程序的默认页面。 Then in your custom UriMapper
you overload the MapUri
method. 然后在自定义UriMapper
,重载MapUri
方法。
public class YourUriMapper : UriMapperBase
{
public override Uri MapUri(Uri uri)
{
if (uri.OriginalString == "/EntryPage.xaml")
{
var settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("WasLaunched"))
{
uri = new Uri("/FirstRunInfoPage.xaml", UriKind.Relative);
}
else
{
uri = new Uri("/MainPage.xaml", UriKind.Relative);
}
}
return uri;
}
}
Then on app initialization you should define which UriMapper
to use: 然后在应用程序初始化时,您应该定义要使用的UriMapper
:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
RootFrame.UriMapper = new YourUriMapper();
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (e.IsApplicationInstancePreserved == false)
{
// tombstoned! Need to restore state
RootFrame.UriMapper = new YourUriMapper();
}
}
The best approach for the check is to write the status in the Isolated storage as you currently are. 最好的检查方法是像现在一样在隔离存储中写入状态。 To redirect to the appropriate page, I would personally use a URI mapper. 要重定向到适当的页面,我个人将使用URI映射器。 that way, your first intended entry page would be in the navigation first entry stack, preventing users from navigating back to the first page. 这样,您的第一个预定条目页面将位于导航第一个条目堆栈中,从而防止用户导航回到第一页。 A typical use case would be to redirect a user to a an authentication page when a user isn't authenticated and to a home page when the user is already authenticated, see This example 一个典型的用例是在用户未通过身份验证时将用户重定向到身份验证页面,而在用户已经通过身份验证时将用户重定向到主页,请参见本示例。
public App()
{
SetUpLandingPageView();
}
void SetUpLandingPageView()
{
var isLaunched = IsolatedStorageSettings.ApplicationSettings.Contains("WasLaunched");
// Get the UriMapper from the app.xaml resources, and assign it to the root frame
var mapper = Resources["mapper"] as UriMapper;
if (mapper == null)
throw new ArgumentNullException("Mapper must be configured");
RootFrame.UriMapper = Resources["mapper"] as UriMapper;
// Update the mapper as appropriate
mapper.UriMappings[0].MappedUri = isLaunched ? new Uri("/Views/HomePage.xaml", UriKind.Relative) : new Uri("/Views/Introduction.xaml", UriKind.Relative);
}
In the app.xaml 在app.xaml中
Namespace: 命名空间:
xmlns:UriMapper="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone"
Xaml Xaml
<Application.Resources>
<ResourceDictionary>
<UriMapper:UriMapper x:Name="mapper">
<UriMapper:UriMapping Uri="/MainPage.xaml" />
</UriMapper:UriMapper>
</ResourceDictionary>
</Application.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.