简体   繁体   中英

Stack panel child not being added dynamically

I've fallen foul of something that brings not errors or warnings but has me stumped. My knowledge level is novice, so bear with me.

While writing a small WPF app to fetch data from Twitch, I needed a stack panel that adds a user control as a child dynamically, but nothing shows up in the stack panel when the code is executed.

I thought the problem may be that the call to add the child comes from another thread, but after testing, the add doesn't work even when on the same thread.

I've not ever had to use a user control in apps before, so I'm curious as to whether that's the issue, but after nearly two days of trying to find the problem ive come here. I know its bound to be a simple error. Apologies in advance if my code is difficult to read here - use long descriptive names for variables and methods.

Here's the code:

Firstly the hard work of the app is done on a separate thread like so:

Thread main_work_thread = new Thread(new ThreadStart(do_main_work));
main_work_thread.IsBackground = false;
main_work_thread.Start();

The children get added to the panel via a quartz job (the trigger works as intended):

 public virtual void Execute(IJobExecutionContext context)
    {
        List<twitch_api.Stream> online_streamers = App.twitch_interface.get_followed_channels_data().streams;

        foreach (twitch_api.Stream streamer in online_streamers)
        {

            // ...collect data


            // display data in window
            App.Current.Dispatcher.Invoke((Action)(() =>
            {
                ((MainWindow)App.Current.MainWindow).favourite_streamer_stack_clear();
                ((MainWindow)App.Current.MainWindow).add_streamer_to_favourite_stack(streamer_detail);
            }));
        }

    }

Here are the methods being called inside the MainWindow:

public void add_streamer_to_favourite_stack(favourite_streamer_list_item_data new_streamer)
    {
        favourite_streamer_stack.Children.Add(new favourite_streamer_list_item_control(new_streamer));
    }

    public void favourite_streamer_stack_clear()
    {
        favourite_streamer_stack.Children.Clear();
    }

And here is the code behind the user control I'm trying to add:

 public partial class favourite_streamer_list_item_control : UserControl
{
   public favourite_streamer_list_item_data my_streamer_data { get; set; }

    public favourite_streamer_list_item_control()
    { 
        InitializeComponent();
    }

    public favourite_streamer_list_item_control(favourite_streamer_list_item_data new_streamer_data)
    {
        this.my_streamer_data = new_streamer_data;
    }

    private void favourite_streamer_stack_item_Loaded(object sender, RoutedEventArgs e)
    {
        // update controls with the data
        this.streamer_avatar.Source = my_streamer_data.streamer_logo_bitmap;
        this.textblock_streamer_name.Text = my_streamer_data.streamer_name;
        this.textblock_game.Text = my_streamer_data.stream_game;
        this.textblock_title.Text = my_streamer_data.stream_title;
    }
}

Many thanks for your time.

You need to call InitializeComponent(); in your parameterless constructor. Without this call controller won't be rendered.

public favourite_streamer_list_item_control(favourite_streamer_list_item_data new_streamer_data)
{
    InitializeComponent();
    this.my_streamer_data = new_streamer_data;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM