【问题标题】:C# WPF Checkbox with null value?具有空值的 C# WPF 复选框?
【发布时间】:2020-01-11 00:15:09
【问题描述】:

我目前正在 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();
    }
}

}

【问题讨论】:

  • 您在 InitializeComponent()(从代码隐藏中的构造函数调用)完成运行之前运行该代码。
  • @canton7 我也是这么想的,这是检查输入是否正确的函数的一部分,这是我用于 (Page) Main Function tho 'public New_Server() { InitializeComponent 的代码(); Owner.Text = Global_Class.GetUsername(Environment.UserName); }' 我什至没有调用这个函数,它是由一个按钮引起的,但由于某种原因仍然表现得很奇怪。
  • 请发布您的完整代码 -- minimal reproducible example
  • 对。我敢打赌Public_Server 是一个复选框,它在构造时会被选中(在 InitializeComponent 中)。在注册Public_Server_Checked 之后,但在构造Password_Enabled 之前检查它。所以它确实发生了,因为 Public_Server_Checked 在 InitializeComponent 完成运行之前运行。
  • 当然,第一名Here

标签: c# wpf checkbox nullreferenceexception


【解决方案1】:

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

【讨论】:

  • 我认为这样可以将某些内容标记为答案,因此如果有人遇到此问题,它就在那里。
猜你喜欢
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2010-10-19
相关资源
最近更新 更多