简体   繁体   English

简单的孤立存储问题

[英]Simple Isolated Storage Problem

I am trying to do a simple test with Isolated Storage so I can use it for a Windows Phone 7 application I am making. 我正在尝试使用独立存储进行简单的测试,因此我可以将它用于我正在制作的Windows Phone 7应用程序。

The test I am creating sets a creates a key and value with one button, and with the other button sets that value equal to a TextBlock's text. 我正在创建的测试设置a用一个按钮创建一个键和值,而另一个按钮设置该值等于TextBlock的文本。

namespace IsoStore
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    public class AppSettings
    {
        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            appSettings.Add("email", "someone@somewhere.com");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = (string)appSettings["email"];
        }
    }      
}
}

This way gives me this error: 这种方式给了我这个错误:

Cannot access a non-static member of outer type 'IsoStore.MainPage' via nested type 'IsoStore.MainPage.AppSettings' 无法通过嵌套类型“IsoStore.MainPage.AppSettings”访问外部类型“IsoStore.MainPage”的非静态成员

So I tried this: 所以我尝试了这个:

namespace IsoStore
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    public class AppSettings
    {
        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            appSettings.Add("email", "someone@somewhere.com");
        }

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = (string)appSettings["email"];
    }
}
}

And instead I get this error: 而我得到这个错误:

The name 'appSettings' does not exist in the current context “appSettings”这个名称在当前上下文中不存在

So what obvious problem am I overlooking here? 那么我在这里忽略了一个明显的问题呢?

Thanks so much for your time. 非常感谢你的时间。

appSettings is out of scope for button2_Click appSettings超出了button2_Click的范围

Update Since IsolatedStorageSettings.ApplicationSettings is Static anyway there's no need for the reference at all. 更新,因为IsolatedStorageSettings.ApplicationSettings是静态的,无论如何根本不需要引用。 Just directly access it. 只需直接访问它。

namespace IsoStore
{

 public partial class MainPage : PhoneApplicationPage
 {


    // Constructor
    public MainPage()
    {
    InitializeComponent();


    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
    IsolatedStorageSettings.ApplicationSettings.Add("email", "someone@somewhere.com");
    }



    private void button2_Click(object sender, RoutedEventArgs e)
    {
       textBlock1.Text = (string)IsolatedStorageSettings.ApplicationSettings["email"];
    }
  }
}

Try this code, as there isn't any need to define AppSettings class. 尝试此代码,因为不需要定义AppSettings类。

namespace IsoStore
{
    public partial class MainPage : PhoneApplicationPage
    {
        IsolatedStorageSettings appSettings;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            appSettings = IsolatedStorageSettings.ApplicationSettings;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            appSettings.Add("email", "someone@somewhere.com");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = (string)appSettings["email"];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM