在 Windows Phone 应用程序中存储和获取数据并使用 NavigationService.GoBack() [英] Storing and getting data in windows phone app and using NavigationService.GoBack()

查看:24
本文介绍了在 Windows Phone 应用程序中存储和获取数据并使用 NavigationService.GoBack()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基于问答游戏的 Windows 手机应用程序.我希望当用户对某些问题给出正确答案时,问题的选项卡中将永久显示一个小刻度线.我想存储每个问题的分数,以便我可以在地名中将其显示为您的分数".即使应用程序关闭,该分数也不会重置.我已使用IsolatedStorageFile 分别存储每个页面的数据.在每个页面中,我都提供了一个后退按钮,该按钮将使用NavigationService.GoBack()"导航到类别页面.

I have created a Windows phone app based on a quiz game. I want that when the user give the correct answer for some question then a small tick mark will be permanently on in the tab of the question. I want to store score for every question so that i can display that in a place name as 'your score'. And that score will not be reset even if the app is closed.I have used IsolatedStorageFile to store the data for each page separately. In each page i have provided a back button which will navigate to category page using "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")不会显示.但是当我再次导航到主页然后回到类别页面时,就会显示技巧.我希望每当我给出正确答案并使用应用程序中提供的后退按钮或移动设备的后退按钮导航回类别页面时,该技巧应仅在当时显示.

The problem is that when ever i gave the correct answer and press the back button which navigate to logo page the trick(named as "ama" in category code) do not show. But when again i navigate to mainpage then come back to category page the trick is shown. I want that when ever i give the correct answer and navigate back to category page using the back button provided in the app or back button of mobile the trick should be shown at that time only.

推荐答案

根据你的代码.您的逻辑位于该 classconstructor 中.一旦您从主页导航到它就会调用它,并且当您从问题页面导航回来时不会再次调用它.因此,从逻辑上讲,当它向后导航时,您不会执行任何代码,因此会显示与原来相同的视图.

According to your code. Your logic is in constructor of that class. Which is called once you navigate to it from main page and is not called again when you navigate back from question page. So, you are logically not executing any code when it navigates back, hence displaying the same view as it was.

要使其更新,请在 Page_Loaded 事件中编写逻辑代码.

To make it update, write your logic code in Page_Loaded event.

这篇关于在 Windows Phone 应用程序中存储和获取数据并使用 NavigationService.GoBack()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆