尝试修改XAML元素时WPF抛出System.NullReferenceException

时间:2018-10-12 18:47:33

标签: c# wpf

我一直在尝试制作一个德国健身计划(试图学习它,但是很困难),但是遇到了“ System.NullReferenceException”错误。知道是什么原因造成的,以及如何解决?谢谢:)

我一直在寻找一种方法来将多个(在这种情况下最多为20个)对象参数更改为某个值,而不使用HUUUGE代码,并且采用疯狂的方式,因为我是一位相当新的程序员,但仍然不知道所有的快捷方式和东西:D

我在互联网上浏览了很多,试图找到一种方法来做,但是我没有成功,所以我在这里问。抱歉,如果我认为它的代码很多,最好将其全部发布,然后错过对我来说似乎并不重要但实际上很重要的事情,对吗? :)

哦,我在“((((clen” + x)as object)as TextBox).IsEnabled = true;“中遇到了异常)部分。但是它认为它也会在我使用类似的部分代码的其他任何地方发生

XAML:

<Window x:Class="NJ.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NJ"
    mc:Ignorable="d"
    Title="MainWindow" Height="900" Width="1600">
<Grid>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" Name="clen1" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" Name="slovicko1" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen2" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko2" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen3" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko3" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="40" Margin="10,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen4" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="427" x:Name="slovicko4" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen5" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko5" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen6" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko6" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen7" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko7" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="40" Margin="10,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen8" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="427" x:Name="slovicko8" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="40" Margin="10,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen9" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="427" x:Name="slovicko9" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="10,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen10" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="106,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko10" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen11" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko11" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen12" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko12" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen13" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko13" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="40" Margin="651,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen14" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="428" x:Name="slovicko14" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen15" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko15" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen16" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko16" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen17" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko17" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="40" Margin="651,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen18" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="428" x:Name="slovicko18" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="40" Margin="651,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen19" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="428" x:Name="slovicko19" IsEnabled="False" FontSize="25"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="651,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen20" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="746,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko20" IsEnabled="False" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="1266,33,0,0" TextWrapping="Wrap" Text="Od lekce:" VerticalAlignment="Top" Height="39" Width="90" FontSize="20"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="1362,31,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="38" Name="zacateklekci" FontSize="25"/>
    <TextBlock HorizontalAlignment="Left" Margin="1426,33,0,0" TextWrapping="Wrap" Text="do lekce:" VerticalAlignment="Top" Height="39" Width="90" FontSize="20"/>
    <TextBox HorizontalAlignment="Left" Height="39" Margin="1520,31,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="38" Name="koneclekci" FontSize="25"/>
    <Slider HorizontalAlignment="Left" Margin="1290,110,0,0" VerticalAlignment="Top" Maximum="20" Height="39" Width="250" TickPlacement="BottomRight" TickFrequency="1" IsSnapToTickEnabled="True" Name="wordcount" Minimum="1" Value="1"/>
    <TextBlock HorizontalAlignment="Left" Margin="1236,109,0,0" TextWrapping="Wrap" Text="{Binding ElementName=wordcount, Path=Value, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Height="40" Width="49" FontSize="25"/>
    <Button Content="Nový test" HorizontalAlignment="Left" Margin="1266,161,0,0" VerticalAlignment="Top" Width="274" Height="39" FontSize="25" Click="Button_Click" Name="generateTest"/>
    <Button Content="Zkontrolovat" HorizontalAlignment="Left" Margin="1266,264,0,0" VerticalAlignment="Top" Width="274" Height="39" FontSize="25" Click="kontroluj" Name="zkontolujbutton"/>
    <TextBlock HorizontalAlignment="Left" Margin="1266,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="274" FontSize="25" Name="pocetchyb"/>

</Grid>

C#邮政编码

public partial class MainWindow : Window
{
    public int chyby = 0;
    public List<string> comparelist = new List<string>();
    public List<string> derlist = new List<string>();
    public List<string> dielist = new List<string>();
    public List<string> daslist = new List<string>();
    public List<string> blacklist = new List<string>();
    int tmp1 = 1;
    int tmp2 = 1;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (Int32.TryParse(zacateklekci.Text, out tmp1) && Int32.TryParse(koneclekci.Text, out tmp2))
        {
            if (tmp1 < 8 && tmp1 > 0 && tmp2 < 8 && tmp2 > 0)
            {
                chyby = 0;
                for (int i = 1; i <= tmp2; i++)
                {
                    using (StreamReader r = new StreamReader(@"C:\Users\Honza\Documents\VS Files\nj\data" + i + "der.json"))
                    {
                        string json = r.ReadToEnd();
                        foreach (string s in JsonConvert.DeserializeObject<List<String>>(json)) derlist.Add(s);
                    }
                    using (StreamReader r = new StreamReader(@"C:\Users\Honza\Documents\VS Files\nj\data" + i + "die.json"))
                    {
                        string json = r.ReadToEnd();
                        foreach (string s in JsonConvert.DeserializeObject<List<String>>(json)) dielist.Add(s);
                    }
                    using (StreamReader r = new StreamReader(@"C:\Users\Honza\Documents\VS Files\nj\data" + i + "das.json"))
                    {
                        string json = r.ReadToEnd();
                        foreach (string s in JsonConvert.DeserializeObject<List<String>>(json)) daslist.Add(s);
                    }
                }
            }
        }
        Random rand = new Random();
        for (int x = 1; x <= (wordcount.Value + 1); x++)
        {
            ((("clen" + x) as object) as TextBox).IsEnabled = true;
            ((("slovicko" + x) as object) as TextBlock).IsEnabled = true;
            int tmp = 0;
            switch (rand.Next(3))
            {
                case 1:
                    comparelist.Add("die");
                    do
                    {
                        tmp = rand.Next((dielist.Count + 1));
                    } while (blacklist.Contains(dielist[tmp]));
                    ((("slovicko" + x) as object) as TextBlock).Text = dielist[tmp];
                    blacklist.Add(dielist[tmp]);
                    break;
                case 2:
                    comparelist.Add("das");
                    do
                    {
                        tmp = rand.Next((daslist.Count + 1));
                    } while (blacklist.Contains(daslist[tmp]));
                    ((("slovicko" + x) as object) as TextBlock).Text = daslist[tmp];
                    blacklist.Add(daslist[tmp]);
                    break;
                default:
                    comparelist.Add("der");
                    do
                    {
                        tmp = rand.Next((derlist.Count + 1));
                    } while (blacklist.Contains(derlist[tmp]));
                    ((("slovicko" + x) as object) as TextBlock).Text = derlist[tmp];
                    blacklist.Add(derlist[tmp]);
                    break;
            }
        }
        generateTest.IsEnabled = false;
        zkontolujbutton.IsEnabled = true;
    }

    public void ClearComparsion()
    {
        comparelist = new List<string>();
        derlist = new List<string>();
        dielist = new List<string>();
        daslist = new List<string>();
        blacklist = new List<string>();
        try
        {
            for (int i = 1; i < 21; i++)
            {
                ((("clen" + i) as object) as TextBox).IsEnabled = false;
                ((("clen" + i) as object) as TextBox).Background = Brushes.Transparent;
                ((("clen" + i) as object) as TextBox).Text = null;
                ((("slovicko" + i) as object) as TextBlock).IsEnabled = false;
                ((("slovicko" + i) as object) as TextBlock).Background = Brushes.Transparent;
                ((("slovicko" + i) as object) as TextBlock).Text = null;
            }
        } catch (Exception e) {}
        generateTest.IsEnabled = true;
        zkontolujbutton.IsEnabled = false;
    }

    private void kontroluj(object sender, RoutedEventArgs e)
    {
        for (int i = 1; i <= (wordcount.Value + 1); i++)
        {
            if (((("clen" + i) as object) as TextBox).Text.ToLower().Equals(comparelist[i]))
            {
                ((("clen" + i) as object) as TextBox).Background = Brushes.LightGreen;
            } else
            {
                ((("clen" + i) as object) as TextBox).Background = Brushes.Pink;
                chyby++;
            }
        }
        ClearComparsion();
    }
}

1 个答案:

答案 0 :(得分:0)

问题出在查找/访问控件的逻辑上,例如((("clen" + x) as object) as TextBox)和类似的代码,其中将对象转换为TextBox

当您说("clen" + x) as object本质上是String对象而不是TextBox对象时。因此,当您将String对象投射到TextBox时,它将导致为空。

要解决此问题,可以使用FindName方法,该方法将在窗口中搜索给定名称的所有元素。请注意,您不能使用它来搜索侧模板中的元素,而您并没有这样做。因此,固定代码如下所示:

(FindName("clen" + x) as TextBox)

确保检查结果是否为空。