UIElementCollection 不能为空.派生自预期 UIElement 的对象

本文关键字:UIElement 对象 不能 派生 UIElementCollection | 更新日期: 2023-09-27 18:31:18

我试图从具有相同属性的代码隐藏创建一个按钮:

<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。

UIElementCollection 不能为空.派生自预期 UIElement 的对象

虽然你的代码真的让我感到困惑,但这是不起作用的。构造完全限定的类型名称的方式为

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

其中type.Namespace已经包含相关类型的完整命名空间,因此不需要"AppPages"部分。相反,你应该写

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

为什么不干脆做

assembly.CreateInstance(type.FullName);

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