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.
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.
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'
So I tried this:
namespace IsoStore
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent开发者_JAVA百科();
}
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
So what obvious problem am I overlooking here?
Thanks so much for your time.
appSettings is out of scope for button2_Click
Update Since IsolatedStorageSettings.ApplicationSettings is Static anyway there's no need for the reference at all. 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.
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"];
}
}
}
精彩评论