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

3

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

Window's XAML (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>

Window C # 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 required I can also post.

Main method (project .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;
        }
    }
}
    
asked by anonymous 23.09.2016 / 16:45

1 answer

2

For this to work you should do the following steps:

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

2º - You should add the reference to DLL you created, if you already created 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();
    }
  }
}

Here's a picture:

Source:#, here and here .

    
23.09.2016 / 22:34