簡體   English   中英

簡單的孤立存儲問題

[英]Simple Isolated Storage Problem

我正在嘗試使用獨立存儲進行簡單的測試,因此我可以將它用於我正在制作的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”這個名稱在當前上下文中不存在

那么我在這里忽略了一個明顯的問題呢?

非常感謝你的時間。

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"];
    }
  }
}

嘗試此代碼,因為不需要定義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