0

I've created a Random Number Generator app. It allows the user to enter integers into min, max, and amount to gen. There are three buttons; generate, clear output, and save. All work except the save.

I've been working on this for about 6 hours now (not joking) and I still can't figure it out. I found this thread, but I don't understand how it is implemented: How to store an integer value in isolated storage in wp7?

Here is the code:

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);
    //}
}

}

If anyone could PLEASE help me, that would be great. Thank you.

4

1 回答 1

1

min1在构造函数中使用而不声明变量。

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

改成

var min1 = IsolatedStorageSettings.ApplicationSettings["minstorage"];
于 2013-06-01T15:56:11.097 回答