我目前正在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"/>
我已经在网上搜索过,但是由于存在此类错误(已标准化),因此我知道该复选框被视为null。但是,尽管在搜索我的代码时还没有找到任何说明原因的信息,但对此的任何帮助将不胜感激,谢谢。
我的C#页面的FullCode是
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();
}
}
}
答案 0 :(得分:1)
与UI上的任何内容交互之前,InitializeComponent必须完成。如果您在WPF完成初始化之前尝试访问代码中的可视元素,则会得到null错误。