如何进行get和set方法以允许从不同页面读取保存文件

时间:2011-07-07 05:58:51

标签: c# windows-phone-7 save isolatedstorage

如何从其他页面获取FileName和FileText1?查看文本页面上的代码表示当前上下文中不存在FileName和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);
            }




        }
    }

1 个答案:

答案 0 :(得分:1)

这两个成员被标记为私有,因此无法从其他任何地方(除了该类)访问。

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

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

然后从您的任何页面中调用它:

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