编程很新,我需要帮助解决问题。启动我的程序后,它会说该程序已停止响应并立即关闭。这是代码本身:
XAML
<Window x:Class="WpfApplication6.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="587.615">
<Grid>
<Label Content="Programme de devinette" HorizontalAlignment="Left" Margin="144,10,0,0" VerticalAlignment="Top" Width="355" Height="56" FontSize="22" FontFamily="Segoe WP Black"/>
<TextBox HorizontalAlignment="Left" Name="BoiteChiffre" Height="23" Margin="228,103,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<Label Content="Entrez un chiffre" HorizontalAlignment="Left" Margin="216,66,0,0" VerticalAlignment="Top" Width="164" FontSize="18"/>
<Button Content="Nombre random" HorizontalAlignment="Left" Margin="77,160,0,0" VerticalAlignment="Top" Width="109" Click="Button_Click"/>
<Button Content="Verification" HorizontalAlignment="Left" Margin="424,160,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.055,0.428" Click="Button_Click_2"/>
<Button Content="Quitter" HorizontalAlignment="Left" Margin="253,201,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
这是与该窗口关联的代码:
namespace WpfApplication6
{
/// <summary>
/// Logique d'interaction pour Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
int random1;
private void Button_Click(object sender, RoutedEventArgs e) //random
{
Random chiffrealeatoire = new Random();
random1 = (chiffrealeatoire.Next(0, 20));
}
private void Button_Click_1(object sender, RoutedEventArgs e) //quit
{
Application.Current.Shutdown();
}
private void Button_Click_2(object sender, RoutedEventArgs e) //veri
{
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (Convert.ToInt32(BoiteChiffre.Text) < random1)
{
MessageBox.Show("Too low");
}
if (Convert.ToInt32(BoiteChiffre.Text) > random1)
{
MessageBox.Show("Too high");
}
else
{
MessageBox.Show("Congratulations");
}
}
}
代码应该有3个按钮:一个退出程序,一个在点击时分配0-20之间的隐藏随机数(用户必须猜测这个数字),另一个检查用户输入的数字是否匹配随机生成的数字。如果数字不匹配,程序会告诉用户他的号码是否太高或太低。正如我之前所说,我的程序在启动后崩溃,我无法找到问题所在。感谢所有的帮助,谢谢。
PS:窗口看起来像这样
答案 0 :(得分:0)
以下代码块执行两项操作:
public void Edit(int Id, string Key, string Value)
{
Article Article = Db.ArticleById(Id);
if (Key.Contains(".")) // in this case, Key = ZoneStock.Id
{
string[] Keys = Key.Split('.');
PropertyInfo Lvl1 = Article.GetType().GetProperty(Keys[0]); //Keys[0] = ZoneStock
Type T = Lvl1.PropertyType;
PropertyInfo Lvl2 = T.GetProperty(Keys[1]); //Keys[1] = Id
// Before this point it works, after ....
Lvl2.SetValue(Lvl1, Convert.ChangeType(Value, Lvl2.PropertyType), null);
Db.Update(ref Article);
}
else
{
// .......
}
Db.Save();
}
移动到private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
,因为您不希望每次文本更改时都进行验证,但只有在用户点击“验证”按钮时才会验证。private void Button_Click_2(object sender, RoutedEventArgs e) //veri
被替换为Convert.ToInt32
,它返回一个布尔值(true / false),表示它成功将文本转换为整数(int)并将其写入num变量(是)或者如果没有这样做(假)。由于int.TryParse
无法转换,return;
强制函数结束执行。您可能希望将其更改为以下内容:
int.TryParse
顺便说一下,在WPF中有一些更好的方法可以进行验证和更好的用户体验,您可以自己谷歌 WPF验证。我帮你找了一些:
答案 1 :(得分:-1)
我在此想到您可能需要在BoiteChiffre字段中设置默认值。 将其设置为0,并确保如果字段为空或不是数字,则不会执行代码。 您最好使用Int23.tryparse()而不是Convert()