【问题标题】:UIElementCollection cannot be null. Object derived from UIElement expected?UIElementCollection 不能为空。预期从 UIElement 派生的对象?
【发布时间】:2023-03-22 10:42:01
【问题描述】:

我正在尝试从后面的代码中创建一个按钮,该按钮具有与此相同的属性:

<Button Content="Find Student" Tag="FindStudent" HorizontalAlignment="Right" x:Name="btnFindStudent" Click="btnGeneral_Click" />

后面的代码:

            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnFindStudent");
            btnFindStudent.Tag = Convert.ToString("FindStudent");
            btnFindStudent.Content = Convert.ToString("View");
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;

问题是当我点击按钮时出现错误

Value cannot be null.
Parameter name: Children of 'System.Windows.Controls.UIElementCollection' cannot be null. Object derived from UIElement expected.

我不太确定缺少什么,这是完整的代码:

public partial class FindStudent : UserControl
{
    private Dictionary<string, UserControl> _userControls = new Dictionary<string, UserControl>();
    public Dictionary<string, UserControl> GetUserControls()
    {
        return _userControls;
    }
    private static readonly Random rand = new Random();
    public FindStudent()
    {
        InitializeComponent();

        List<string> userControlKeys = new List<string>();
        userControlKeys.Add("FindStudent");
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        foreach (string userControlKey in userControlKeys)
        {
            string userControlFullName = String.Format("{0}.AppPages.{1}", type.Namespace, userControlKey);
            UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
            _userControls.Add(userControlKey, userControl);
        }
        for (int i = 1; i <= 10; i++)
        {
            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnFindStudent");
            btnFindStudent.Tag = Convert.ToString("FindStudent");
            btnFindStudent.Content = Convert.ToString("View {0}", i);
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(btnFindStudent);
            MainArea.Children.Add(stackPanel);
        }
    }
    private void btnGeneral_Click(object sender, RoutedEventArgs e)
    {
        PanelMainContent.Children.Clear();
        Button button = (Button)e.OriginalSource;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;

        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
        // error on above line. 
    }
}

Xaml:

<UserControl x:Class="WpfApplication4.AppPages.FindStudent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DockPanel HorizontalAlignment="Stretch" >
        <ScrollViewer VerticalScrollBarVisibility="Hidden">

            <DockPanel x:Name="PanelMainWrapper" Margin="10,20,0,0">
                <DockPanel x:Name="PanelMainContent" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,30,0,0">
                    <ScrollViewer VerticalScrollBarVisibility="Hidden">
                        <WrapPanel x:Name="MainArea" VerticalAlignment="Bottom" HorizontalAlignment="Left">

                        </WrapPanel>
                    </ScrollViewer>
                </DockPanel>
            </DockPanel>

        </ScrollViewer>
    </DockPanel>
</UserControl>

它应该只是重新加载我当前使用的页面,我可以在以后设置 appPage,但它不想玩球。 这在我的主窗口中有效,但不在我的控件中,但我没有从后面的代码中设置按钮,我使用了我在主窗口中第一次提到的 xaml。这告诉我我在单击按钮时遗漏了一些东西(我认为设置正确),标签设置正确但我不确定名称是否会像我的 xaml 代码一样是 x:Name。

【问题讨论】:

  • 当您单击其中一个按钮时发生错误?然后发布里面的内容btnGeneral_Click()
  • 嘿,是的,当我单击按钮时会发生这种情况,对不起,我忘了提到错误所在的行,@Morawski 是的,它为空,但我不知道为什么,我只能认为我没有设置其中一个我在 xaml 按钮代码中的目标属性。
  • 我猜assembly.CreateInstance(userControlFullName)(你在userControlKeys上循环)返回null
  • @HenkHolterman 我不确定我是否关注?我发布的代码包含 btnGeneral_Click 信息。

标签: c# wpf xaml custom-controls uielement


【解决方案1】:

虽然您的代码确实让我感到困惑,但这是行不通的。您通过

构建完全限定的类型名称
String.Format("{0}.AppPages.{1}", type.Namespace, userControlKey)

其中type.Namespace 已经包含所讨论类型的完整命名空间,因此“AppPages”部分是不必要的。相反,你应该写

String.Format("{0}.{1}", type.Namespace, userControlKey)

为什么不干脆做

assembly.CreateInstance(type.FullName);

当您只想创建FindStudent 的新实例时(我猜这就是您想要做的)。

【讨论】:

  • 如果我尝试:String.Format("{0}.{1}", type.Namespace, userControlKey) 我得到一个 stackoverflow 异常
  • 这显然是因为您在FindStudent 的构造函数中创建了FindStudent 的新实例,它再次调用构造函数,依此类推。您打算使用该代码实现什么目标?
  • 嗯,那是我的第一个 stackoverflow 异常 :) 现在也许我可以成为这里的俱乐部的一员!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多