How to call and display a WPF Window that is in a dll

Asked

Viewed 1,240 times

3

I have a DLL containing a WPF window, created through a project of the type WPF User Control Library. How do I display this window from another project?

XAML window (DLL project):

<Window x:Class="UmaJanelaComUmBotao.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UmaJanelaComUmBotao"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="112,55,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

C# window code (DLL project):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace UmaJanelaComUmBotao
{
    /// <summary>
    /// Lógica interna para Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1 ( )
        {
            InitializeComponent ( );
        }
    }
}

If codebehind is needed I can also post.

Main method (design . exe)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UmaJanelaComUmBotao;

namespace OExcutavel
{
    class Program
    {
        static void Main ( string[ ] args )
        {
            //Como chamar Window1 a partir daqui
        }
    }
}

codebehind:

namespace UmaJanelaComUmBotao {


    /// <summary>
    /// Window1
    /// </summary>
    public partial class Window1 : System.Windows.Window, System.Windows.Markup.IComponentConnector {


        #line 10 "..\..\Window1.xaml"
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
        internal System.Windows.Controls.Button button;

        #line default
        #line hidden

        private bool _contentLoaded;

        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("/UmaJanelaComUmBotao;component/window1.xaml", System.UriKind.Relative);

            #line 1 "..\..\Window1.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);

            #line default
            #line hidden
        }

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
            switch (connectionId)
            {
            case 1:
            this.button = ((System.Windows.Controls.Button)(target));
            return;
            }
            this._contentLoaded = true;
        }
    }
}
  • Tried a new App().Run();? Has a App.Xaml configured? The StartupUri is with the Window1?

  • Worst of all, I haven’t tried any of that, I don’t even know where to start, all the literature doesn’t talk about how to do it like that. They often let the IDE manage everything. I don’t have an App.xaml pq the executable project is of the type Console Application. I have a line referring to URI in codebehind, I will edit and post it.

  • WPF and Console do not match.

  • @Matheussaraiva, you have already generated the DLL ?

1 answer

2


For this to work you must do the following steps:

1º - Add the following references in the console project: PresentationFramework, WindowBase and PresentationCore.

2º - You must add the reference to DLL that you created, in case you have already created the DLL.

I did a basic test with this code and it’s working:

using System;
using WpfApplication4;

namespace ConsoleApplication7
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
        MainWindow main = new MainWindow();
        main.ShowDialog();
    }
  }
}

Follow picture:

inserir a descrição da imagem aqui

Source: Here, here and here.

  • I’ll test and give feedback.

  • Yes, I marked it as answered because it really worked. But it wasn’t quite the result I had in mind. When running, a console window opens along with the window, and I didn’t want that. It is probably due to the console type design as @bigown quoted. My idea is to leave the entire application front-end outside the executable, ie within a dll. The executable should be "donkey", serving only to startar the application. Bac-Kend and front-end would be in dlls. So I thought of a console project, because it is the most basic executable that exists.

  • I get it, give a layered project research, then you can do whatever you want. Roughly speaking, you create an "exe" project with wpf, and another class library type project with its business rules.

  • I understand, I already know the layer model, but in this case you cite the front-end or at least part of it (Main Window, or a splash) would be in the executable so I would run away from my idea that is to have 0 GUI in the executable. GUI and business rules would be in DLLs distinct. Most people use the trivial that is the GUI packaged with the executable. But I’m beginning to think it’s impossible to escape from it.

  • I think maybe the way is that, that is, really the file App.xaml as quoted the mustache quoted.

  • I found some material that can help: http://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.