【问题标题】:Storing and getting data in windows phone app and using NavigationService.GoBack()在 Windows Phone 应用程序中存储和获取数据并使用 NavigationService.GoBack()
【发布时间】:2014-05-07 07:58:13
【问题描述】:

我创建了一个基于问答游戏的 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”)不会显示。但是当我再次导航到主页然后回到类别页面时,就会显示出窍门。我希望当我给出正确答案并使用应用程序中提供的后退按钮或移动设备的后退按钮导航回类别页面时,技巧应该只在那个时候显示。

【问题讨论】:

标签: windows-phone-8 windows-phone windows-applications


【解决方案1】:

根据您的代码。 您的逻辑在 constructor 中的 class 中。一旦您从主页导航到它就会调用它,当您从问题页面导航回来时不会再次调用它。 因此,当它导航回来时,您在逻辑上没有执行任何代码,因此显示的视图与原来相同。

要使其更新,请将您的逻辑代码写入Page_Loaded 事件。

【讨论】:

  • 在你的构造函数中.. this.Loaded +=
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多