我正在尝试使用独立存储进行简单测试,因此我可以将其用于我正在制作的Windows Phone 7应用程序。
我正在创建的测试设置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"];
}
}
}
}
这种方式给了我这个错误:
无法通过嵌套类型'IsoStore.MainPage.AppSettings'访问外部类型'IsoStore.MainPage'的非静态成员
所以我尝试了这个:
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"];
}
}
}
相反,我得到了这个错误:
当前上下文中不存在名称'appSettings'
那么我在这里忽略了一个明显的问题?
非常感谢你的时间。
答案 0 :(得分:4)
appSettings超出了button2_Click
的范围更新由于IsolatedStorageSettings.ApplicationSettings无论如何都是静态的,根本不需要引用。只需直接访问它。
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"];
}
}
}
答案 1 :(得分:0)
尝试此代码,因为不需要定义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"];
}
}
}