Dan*_*Dan 6 c# isolatedstorage visual-studio-2010 windows-phone-7
我正在尝试使用独立存储进行简单的测试,因此我可以将它用于我正在制作的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"];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方式给了我这个错误:
无法通过嵌套类型"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"];
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我得到这个错误:
"appSettings"这个名称在当前上下文中不存在
那么我在这里忽略了一个明显的问题呢?
非常感谢你的时间.
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"];
}
}
}
Run Code Online (Sandbox Code Playgroud)