首页 > 解决方案 > 具有空值的 C# WPF 复选框?

问题描述

我目前正在 WPF 中制作 GUI,并且正在处理旨在创建和配置消息室的页面,但下面的代码如下

else if (Password_Enabled.IsChecked == true && Password.Text.Length == 0)
        {
            return false; 
        }

出现错误

Password_Enabled.IsChecked 'Password_Enabled.IsChecked' 引发了“System.NullReferenceException”类型的异常

CheckBox 的我的 Xaml 如下所示

<CheckBox x:Name="Password_Enabled" IsChecked="False" Content="Password Enabled" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="Password_Enabled_Checked" Unchecked="Password_Disabled_Checked" Margin="10,5,0,0" Grid.RowSpan="2"/>

我在网上搜索过,但是由于出现了这样的错误,它是标准化的,我知道这意味着该复选框被视为空。但是,在搜索我的代码时没有找到任何说明原因的东西,对此的任何帮助将不胜感激,谢谢。

编辑

我的页面 C# 的完整代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ChatRoom
{
   /// <summary>
   /// Interaction logic for New_Server.xaml
   /// </summary>
 public partial class New_Server : Page
{
    public New_Server()
    {
        InitializeComponent();
        Owner.Text = Global_Class.GetUsername(Environment.UserName);
    }
    public static string ChosenAddress = "";

    private bool CheckValidCredentials()
    {
        List<string> IllegalCharacters = new List<string>() { ",", "|", "\\", "/", ".", "?", "\"", "<", ">", ":" };
        bool Illegal = false;
        if (ServerName.Text.Length == 0)
        {
            return false;
        }
        foreach (string Check in IllegalCharacters)
        {
            if (ServerName.Text.Contains(Check))
            {
                Illegal = true;
                break;
            }
        }
        if (Illegal)
        {
            return false;
        }
        else if (Password_Enabled.IsChecked == true && Password.Text.Length == 0)
        {
            return false; 
        }
        else if (ChosenAddress == "")
        {
            return false; 
        }
        else
        {
            return true; 
        }
    }
    public void SetMakeServer()
    {
        if (CheckValidCredentials())
        {
            MakeServer.IsEnabled = true;
        }
        else
        {
            MakeServer.IsEnabled = false;
        }
    }
    private void Public_Server_Checked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("This Server Will Be Open to everyone in the College, Please Untick if you wish to change this.");
        CheckValidCredentials();
    }
    private void Password_Enabled_Checked(object sender, RoutedEventArgs e)
    {
        Password.IsEnabled = true;
        OneTimePass.IsEnabled = true;
        SetMakeServer();
    }
    private void Password_Disabled_Checked(object sender, RoutedEventArgs e)
    {
        Password.IsEnabled = false;
        OneTimePass.IsEnabled = false;
        SetMakeServer();
    }

    private void Back_Click(object sender, RoutedEventArgs e)
    {
        this.NavigationService.Navigate(new Server_Selection());
    }

    private void ServerDirectorySet_Click(object sender, RoutedEventArgs e)
    {
        using (var fbd = new System.Windows.Forms.FolderBrowserDialog())
        {
            System.Windows.Forms.DialogResult result = fbd.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                ChosenAddress = fbd.SelectedPath;
                ServerDirectoryDisplay.Content = "Location: " + ChosenAddress;
            }
            else
            {
                ChosenAddress = "";
                SetMakeServer();
            }
        }
    }

    private void ServerName_TextChanged(object sender, TextChangedEventArgs e)
    {
        SetMakeServer();
    }

    private void Password_TextChanged(object sender, TextChangedEventArgs e)
    {
        SetMakeServer();
    }
}

}

标签: c#wpfcheckboxnullreferenceexception

解决方案


InitializeComponent 必须在您与 UI 上的任何内容交互之前完成。如果您在 WPF 完成初始化之前尝试访问代码中的可视元素,您将收到 null 错误。


推荐阅读