我创建了一个随机数生成器应用程序。它允许用户输入整数的最小值,最大值和数量。有三个按钮;生成,清除输出并保存。除了保存以外的所有工作。
我现在已经工作了大约6个小时(不是在开玩笑),我仍然无法理解。我找到了这个帖子,但我不明白它是如何实现的: How to store an integer value in isolated storage in 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);
//}
}
}
如果有人能帮助我,那就太好了。谢谢。
答案 0 :(得分:1)
您在构造函数中使用min1
而未声明变量。
min1 = (int)IsolatedStorageSettings.ApplicationSettings["minstorage"];
更改为
var min1 = IsolatedStorageSettings.ApplicationSettings["minstorage"];