In your ui xml change
<object class="GtkWindow" id="foo">
to
<object class="GtkApplicationWindow" id="foo">
and after retrieving the window via gtk builder set the application property:
GTK_WINDOW (gtk_builder_get_object (xml, "foo"));
g_object_set(main_window, "application", application, NULL);