使用IsolatedStorage存储文本框文本,然后在每次打开应用程序时填写字段

本文关键字:文本 应用程序 写字段 存储 IsolatedStorage 使用 然后 | 更新日期: 2023-09-27 18:16:19

我创建了一个随机数生成器应用程序。它允许用户输入整数为min, max和amount。有三个按钮;生成、清除输出并保存。

我已经在这个问题上工作了大约6个小时了(不是开玩笑),我仍然不能弄清楚。我发现这个线程,但我不明白它是如何实现的:如何存储一个整数值在隔离存储在wp7?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Random_Number_Generator_by_Bailey.Resources;
using System.Windows.Input;
using System.IO.IsolatedStorage;
namespace Random_Number_Generator_by_Bailey
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            if (IsolatedStorageSettings.ApplicationSettings.Contains("minstorage"))
            {
                min1 = (int)IsolatedStorageSettings.ApplicationSettings["minstorage"];
                string min1text = min1.ToString();
                MinNumInput.Text = min1text;
            }
            // Retrieve settings
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
    private int randomNumber(int min, int max)
    {
        Random random = new Random(Guid.NewGuid().GetHashCode());
        return random.Next(min, max);
    }
    public void Randomize_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
    {
        int tester2;
        if (!Int32.TryParse(MinNumInput.Text, out tester2))
        {
            OutputBox.Text = "You did not specify an integer for '"Minimum Number'" ";
            return;
        }
        int tester3;
        if (!Int32.TryParse(MaxNumInput.Text, out tester3))
        {
            OutputBox.Text = "You did not specify an integer for '"Maximum'" ";
            return;
        }
        int tester;
        if (!Int32.TryParse(AmountToGenInput.Text, out tester))
        {
            OutputBox.Text = "You did not specify an integer for '"Amount to generate'" ";
            return;
        }
        int gen;
        gen = Convert.ToInt32(AmountToGenInput.Text);
        gen = int.Parse(AmountToGenInput.Text);
        int min1;
        min1 = Convert.ToInt32(MinNumInput.Text);
        min1 = int.Parse(MinNumInput.Text);
        int max1;
        max1 = Convert.ToInt32(MaxNumInput.Text);
        max1 = int.Parse(MaxNumInput.Text);
        if (gen <= 100 && gen > 0)
        {
            for (int i = 0; i < gen; i++)
            {
                int random = randomNumber(min1, max1);
                OutputBox.Text += "  " + random;
            }
        }
        else
        {
            if (gen > 100) { OutputBox.Text = "You cannot generate more than 100 numbers."; }
            if (gen < 0) { OutputBox.Text = "You cannot generate less than 0 numbers."; }
        }
    }
    private void ClearTextBox_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
    {
        OutputBox.Text = "";
    }
    private void SaveParameters(object sender, System.Windows.Input.GestureEventArgs e)
    {
        int gen;
        gen = Convert.ToInt32(AmountToGenInput.Text);
        gen = int.Parse(AmountToGenInput.Text);
        int min1;
        min1 = Convert.ToInt32(MinNumInput.Text);
        min1 = int.Parse(MinNumInput.Text);
        int max1;
        max1 = Convert.ToInt32(MaxNumInput.Text);
        max1 = int.Parse(MaxNumInput.Text);
        //If the variables need to be strings:
        //string gen = AmountToGenInput.Text;
        //string min1 = MinNumInput.Text;
        //string max1 = MaxNumInput.Text;
        // Store the value
        IsolatedStorageSettings.ApplicationSettings["minstorage"] = min1;
    }
    // Sample code for building a localized ApplicationBar
    //private void BuildLocalizedApplicationBar()
    //{
    //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
    //    ApplicationBar = new ApplicationBar();
    //    // Create a new button and set the text value to the localized string from AppResources.
    //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
    //    appBarButton.Text = AppResources.AppBarButtonText;
    //    ApplicationBar.Buttons.Add(appBarButton);
    //    // Create a new menu item with the localized string from AppResources.
    //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
    //    ApplicationBar.MenuItems.Add(appBarMenuItem);
    //}
}

}

如果有人能帮助我,那就太好了。谢谢你。

使用IsolatedStorage存储文本框文本,然后在每次打开应用程序时填写字段

您在构造函数中使用min1而没有声明变量。

min1 = (int)IsolatedStorageSettings.ApplicationSettings["minstorage"];

改变

var min1 = IsolatedStorageSettings.ApplicationSettings["minstorage"];