对象引用未设置 app.Current.Properties 的 wpf 问题

本文关键字:wpf 问题 Properties Current 设置 app 对象引用 | 更新日期: 2023-09-27 18:31:36

所以我有一些学生记录,它从后面的代码中添加内容,我正在尝试将字符串从一个用户控件传递到另一个用户控件:

    private Dictionary<string, UserControl> _userControls = new Dictionary<string, UserControl>();
    public Dictionary<string, UserControl> GetUserControls()
    {
        return _userControls;
    }
    public FindStudent()
    {
        InitializeComponent();
        List<string> userControlKeys = new List<string>();
        userControlKeys.Add("ViewStudent");
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        foreach (string userControlKey in userControlKeys)
        {
            string userControlFullName = String.Format("{0}.{1}", type.Namespace, userControlKey);
            UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
            _userControls.Add(userControlKey, userControl);
        }
        string uriGroups = "http://localhost:8000/Service/Student";
        XDocument xDoc = XDocument.Load(uriGroups);
        var sortedXdoc = xDoc.Descendants("Student")
                       .OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));
        foreach (var node in xDoc.Descendants("Student").OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value)))
        {
            GroupBox groupbox = new GroupBox();
            groupbox.Header = String.Format(node.Element("StudentID").Value);
            App.Current.Properties["groupboxHeader"] = groupbox.Header;
            // when button is clicked it should get the header which is set to studentID
            groupbox.Width = 100;
            groupbox.Height = 100;
            groupbox.Margin = new Thickness(1);
            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnViewStudent");
            btnFindStudent.Tag = Convert.ToString("ViewStudent");
            btnFindStudent.Content = Convert.ToString("View");
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;
            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(groupbox);
            stackPanel.Children.Add(btnFindStudent);
            stackPanel.Margin = new Thickness(5);
            MainArea1.Children.Add(stackPanel);
        }
    }
    private void btnGeneral_Click(object sender, RoutedEventArgs e)
    {
        PanelMainContent.Children.Clear();
        Button button = (Button)e.OriginalSource;
        //PanelMainWrapper.Header = button.Content;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
    }

然后在我看来,学生应该将文本块设置为学生 ID 是什么:

public partial class ViewStudent : UserControl
{
    string myProperty = (string)App.Current.Properties["groupboxHeader"];
    public ViewStudent()
    {
    }
    protected override void OnInitialized(EventArgs e)
    {
        textBlock1.Text = myProperty; // error occurs on this line

但它并没有给出一个错误,指出对象引用未设置为对象的实例。

我认为可能存在以下问题:

App.Current.Properties["groupboxHeader"] = groupbox.Header;

我不认为它做了我需要它做的事情,那就是在我单击动态添加的按钮时将 app.properties 设置为当前的 groupbox 标题。

编辑:

<UserControl x:Class="WpfApplication4.AppPages.ViewStudent"
             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">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="180,56,0,0" Name="textBlock1" VerticalAlignment="Top" />
   </Grid>
</UserControl>

对象引用未设置 app.Current.Properties 的 wpf 问题

如果显示的代码是完整的,你只是忘记在ViewStudent构造函数中调用InitializeComponent。因此textBlock1null.

public ViewStudent() 
{ 
    InitializeComponent(); // add this
} 

并且永远不要忘记在OnInitialized中调用基类:

protected override void OnInitialized(EventArgs e)
{
    base.OnInitialized(e); // and add this
    ...
}