我根据问答游戏创建了一个Windows手机应用程序。我希望当用户为某些问题提供正确答案时,问题标签中的小刻度标记将永久显示。 我想为每个问题存储分数,以便我可以在地名中显示为“你的分数”。即使应用程序已关闭,该分数也不会重置。我使用IsolatedStorageFile分别存储每个页面的数据。在每个页面中,我都提供了一个后退按钮,它将使用“NavigationService.GoBack()”导航到类别页面。
//mainpage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using logic.Resources;
using System.IO;
using System.IO.IsolatedStorage;
namespace logic
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/logo.xaml", UriKind.Relative));
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
int y = 0;
if (!fileStorage.FileExists("amazon.txt"))
{
//Create a new StreamWriter, to write the file to the specified location.
StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("amazon.txt", FileMode.OpenOrCreate, fileStorage));
//Write the contents of our TextBox to the file.
fileWriter.WriteLine(y);
//Close the StreamWriter.
fileWriter.Close();
}
}
}
}
//类别页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.IO.IsolatedStorage;
namespace logic
{
public partial class logo : PhoneApplicationPage
{
public logo()
{
InitializeComponent();
int x = 0;
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new StreamReader
StreamReader fileReader = null;
if (fileStorage.FileExists("amazon.txt"))
{
//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("amazon.txt", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
string textFile = fileReader.ReadLine();
x = Convert.ToInt32(textFile);
//Write the contents of the file to the TextBlock on the page.
fileReader.Close();
if (x == 1)
{
ama.Visibility = System.Windows.Visibility.Visible;
}
}
}
private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
NavigationService.Navigate(new Uri("/amazon.xaml", UriKind.Relative));
}
}}
//问题页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.IO.IsolatedStorage;
namespace logic
{
public partial class amazon : PhoneApplicationPage
{
public amazon()
{
InitializeComponent();
}
//check the answer is correct or not
int count = 0;
int x;
private void b1_Click(object sender, RoutedEventArgs e)
{
if (tb1.Text.Length == 0 || tb2.Text.Length == 0 || tb3.Text.Length == 0 || tb4.Text.Length == 0 || tb5.Text.Length == 0 || tb6.Text.Length == 0)
{
}
else
{
Char s1, s2, s3, s4, s5, s6;
s1 = Char.ToUpper(tb1.Text[0]);
s2 = Char.ToUpper(tb2.Text[0]);
s3 = Char.ToUpper(tb3.Text[0]);
s4 = Char.ToUpper(tb4.Text[0]);
s5 = Char.ToUpper(tb5.Text[0]);
s6 = Char.ToUpper(tb6.Text[0]);
if (s1 == 'A' && s2 == 'M' && s3 == 'A' && s4 == 'Z' && s5 == 'O' && s6 == 'N')
{
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new StreamReader
StreamReader fileReader = null;
//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("amazon.txt", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
string textFile = fileReader.ReadLine();
x = Convert.ToInt32(textFile);
//Write the contents of the file to the TextBlock on the page.
fileReader.Close();
if (x == 0)
{
fileStorage.DeleteFile("amazon.txt");
//Create a new StreamWriter, to write the file to the specified location.
StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("amazon.txt", FileMode.OpenOrCreate, fileStorage));
fileWriter.WriteLine("1");
//Close the StreamWriter.
fileWriter.Close();
}
ans.Text = "Correct!!!";
}
else
{
ans.Text = "\n\nINCORRECT";
}
}
}
//reset the value
private void reset_Click(object sender, RoutedEventArgs e)
{
tb1.Text = "";
tb2.Text = ""; tb3.Text = ""; tb4.Text = ""; tb5.Text = ""; tb6.Text = "";
}
private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
NavigationService.GoBack();
}
}
}
问题在于,当我给出正确的答案并按下导航到徽标页面的后退按钮时,诀窍(在类别代码中命名为“ama”)不会显示。但是当我再次导航到主页然后回到类别页面时,显示了技巧。我想要的是,当我给出正确的答案并使用应用程序中提供的后退按钮或移动设备的后退按钮导航回类别页面时,该诀窍应该仅在那时显示。
答案 0 :(得分:0)
根据你的代码。
您的逻辑位于constructor
的{{1}}。一旦从主页面导航到它,就会调用它,当您从问题页面导航回来时不再调用它。
因此,在逻辑上,当它向后导航时,你没有执行任何代码,因此显示的视图与原来的相同。
要进行更新,请在class
事件中编写逻辑代码。