开发者

how to do a get and set method to allow reading of save files from different page

开发者 https://www.devze.com 2023-03-18 02:20 出处:网络
How do i get the FileName and FileText1 from the other page? The codes on the Viewing Text Page says that the FileName and FileText1 doesn\'t exist in the current context.

How do i get the FileName and FileText1 from the other page? The codes on the Viewing Text Page says that the FileName and FileText1 doesn't exist in the current context.

This is the codes for the creating page to save the written text:

namespace WindowsPhoneApplication1
{
    public partial class CreateQuizPage : PhoneApplicationPage
    {
        public CreateQuizPage()
        {
            InitializeComponent();
        }

        private const string FileName = "Name";
        private const string FolderName = "QuestionFolder";
        private string FilePath = System.IO.Path.Combine(FolderName, FileName);



        private void Button_Click(object sender, RoutedEventArgs e)
        {

            this.OnSaveFile(FilePath);


            MessageBox.Show("File saved successfully");

            NavigationService.Navigate(new Uri("/CompleteQuizPage.xaml", UriKind.Relative));

        }



        private void OnSaveFile(string filePath)
        {
            StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string directoryName = System.IO.Path.GetDirectoryName(filePath);
                if (!string.IsNullOrEmpty(directoryName) && !myIsolatedStorage.DirectoryExists(directoryName))
                {
                    myIsolatedStorage.CreateDirectory(directoryName);
                }

                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter writer = new StreamWriter(开发者_运维问答fileStream))
                    {

                        string someTextData = textFileName.Text + text1.Text;
                        writer.WriteLine(someTextData);
                    }
                }
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }


    }
}

This is the codes for the viewing page to view the text:

namespace WindowsPhoneApplication1
    {
        public partial class AnswerQuestionPage : PhoneApplicationPage
        {

            public AnswerQuestionPage()
            {
                InitializeComponent();
                }





            private void OnReadSelected(string filePath)
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(filePath))
                    {
                        using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                        {
                            using (StreamReader reader = new StreamReader(fileStream))
                            {
                                this.titleText.Text = reader.ReadLine();
                            }
                        }
                    }
                    else
                    {
                        //MessageBox.Show("File not found!");

                    }
                }
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {



                NavigationService.Navigate(new Uri("/CompleteAnswerPage.xaml", UriKind.Relative));
            }

            private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {

                this.OnReadSelected(FileName);
                this.OnReadSelected(FileText1);
            }




        }
    }


Those two members are marked as private so can not be accessible from anywhere else (other than that class).

A better way of doing this is to create a static class for your constants and use it from all of your pages.

public static class Constants
{
    public const string FileName = "Name";
    public const string FolderName = "QuestionFolder";
}

and then call it like this from any of your pages:

            this.OnReadSelected(Constants.FileName);
            this.OnReadSelected(Constants.FileText1);
0

精彩评论

暂无评论...
验证码 换一张
取 消