C# 如何使用get和set方法从不同页面读取保存文件

C# 如何使用get和set方法从不同页面读取保存文件,c#,windows-phone-7,save,isolatedstorage,C#,Windows Phone 7,Save,Isolatedstorage,如何从另一个页面获取文件名和FileText1?查看文本页面上的代码表示当前上下文中不存在文件名和FileText1 以下是用于创建页面以保存书面文本的代码: namespace WindowsPhoneApplication1 { public partial class CreateQuizPage : PhoneApplicationPage { public CreateQuizPage() { Initialize

如何从另一个页面获取文件名和FileText1?查看文本页面上的代码表示当前上下文中不存在文件名和FileText1

以下是用于创建页面以保存书面文本的代码:

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)
        {

        }


    }
}
这是查看页面查看文本的代码:

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);
            }




        }
    }

这两个成员被标记为private,因此不能从其他任何地方(该类除外)访问

更好的方法是为常量创建一个静态类,并从所有页面使用它

public static class Constants
{
    public const string FileName = "Name";
    public const string FolderName = "QuestionFolder";
}
然后在您的任何页面中这样称呼它:

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

我收到了类似“WindowsPhoneApp1.Model.Constants.FileName由于其保护级别而无法访问”和“WindowsPhoneApp1.Model.Constants不包含FileText1的定义”的错误。这就是我收到的错误,我不确定如何解决它。抱歉,更改了它,(这是一个复制粘贴问题),当然它们需要公开。确定,我明白你的意思。更改后,我仍然收到错误“WindowsPhoneApp1.Model.Constants不包含FileText1的定义”。因为您必须在其中添加另一个名为“FileText1”的字段,因此当前仅在其中定义了FileName和FolderName。