如何检查变量是否已经声明? (隔离存储)
我正在用 C# 为 Windows Phone 7 编写一个应用程序。 在此应用程序中,我有一些设置(语言和音频)。 首次启动时,应用程序允许您在 MainPage.xaml(起始页)中选择语言并将该值保存在isolatedStorage 中。 下次启动时,应用程序会使用用户选择的语言在开始菜单上自动运行。问题是我无法检查isolatedStorage中的值,因为它是在您在MainPage.xaml上选择语言时声明的
,这就是我所做的:
在App.xaml中我使用:
RootFrame.Navigating += new NavigatingCancelEventHandler(MainPage.RootFrame_Navigating);
在MainPage.xaml中我有程序RootFrame_Navigating
:
public static void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains("/MainPage.xaml") != true)
return;
// Change the current navigation
e.Cancel = true;
App.RootFrame.Dispatcher.BeginInvoke(delegate
{
string confronto = (string)MainPage.Impostazioni.appSettings["lingua"];
if ((confronto == "italiano") || (confronto == "english"))
App.RootFrame.Navigate(new Uri("/MainPage.xaml?method=cancel%20navigation&time=", UriKind.Relative));
else
App.RootFrame.Navigate(new Uri("/StartMenu.xaml?method=cancel%20navigation&time=", UriKind.Relative));
});
}
这就是我保存语言的方式:
private void engLink_Click(object sender, RoutedEventArgs e)
{
Impostazioni.appSettings.Add("language", "english");
}
private void itaLink_Click(object sender, RoutedEventArgs e)
{
Impostazioni.appSettings.Add("language", "italiano");
}
错误是KeyNotFoundException
,因为我在声明之前使用了这个变量,我想......怎么可能我修好这个吗?
谢谢你!
I'm writing an application in C# for Windows Phone 7.
In this application I have some Settings (language and audio).
On the first boot, the application lets you choose, in MainPage.xaml (Start Page) the language and saves the value in the IsolatedStorage.
On the next boots, the application runs automatically on the start menu with the language chosen by the user. The problem is that I can't check the value in the IsolatedStorage, because it's declared when you choose the language on the MainPage.xaml
That's what I did:
In App.xaml I use:
RootFrame.Navigating += new NavigatingCancelEventHandler(MainPage.RootFrame_Navigating);
In MainPage.xaml I have the procedure RootFrame_Navigating
:
public static void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains("/MainPage.xaml") != true)
return;
// Change the current navigation
e.Cancel = true;
App.RootFrame.Dispatcher.BeginInvoke(delegate
{
string confronto = (string)MainPage.Impostazioni.appSettings["lingua"];
if ((confronto == "italiano") || (confronto == "english"))
App.RootFrame.Navigate(new Uri("/MainPage.xaml?method=cancel%20navigation&time=", UriKind.Relative));
else
App.RootFrame.Navigate(new Uri("/StartMenu.xaml?method=cancel%20navigation&time=", UriKind.Relative));
});
}
And this is how I save the language:
private void engLink_Click(object sender, RoutedEventArgs e)
{
Impostazioni.appSettings.Add("language", "english");
}
private void itaLink_Click(object sender, RoutedEventArgs e)
{
Impostazioni.appSettings.Add("language", "italiano");
}
The error is KeyNotFoundException
because I'm using this variable before it's declared, I suppose ... How can I fix this?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种方法(
Contains()
)就是为了这个目的。There is a method (
Contains()
) for just this purpose.在 App.xaml 中创建设置喜欢并将其保留为空。
在此之后,您将能够使用您的这段代码,
希望它能起作用。
in App.xaml create setting liks and just keep it blank.
after this u will be able to use this code of yours
hopefully it ll work.