简体   繁体   中英

Wpf dll user control window not showing when called from an exe application

I have a WPF Menu application wich calls a WPF User Control dll and the dll window is not showing. The dll does not require parameters and its window have only one button. I added a MessageBox to the dll code to check if it is being loaded and it does, but the window does not show.

I'm using VS 2015. The dll project named Empresa.Reg was created using C# Windows Classic Desktop WPF User Control (There is no C# Windows WPF User Control selection), it is referenced in the Menu project and its ouput is Class Library. Menu project named MenuDePruebas Ouput is Windows Application. Any help would be appreciated.

This is the Menu exe code:

....

using System.Windows;
using Empresa.Reg;

namespace MenuDePruebas

{
   public partial class MainWindow : Window

{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void BtnRegEmpresa_Click(object sender, RoutedEventArgs e)
    {
        UserControl1 algo = new UserControl1();
    }
}

}

And this is the WPF User Control dll code:

....

using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Empresa.Reg
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        MessageBox.Show("Testing");  // This line works as expected

    }
    private void button_Click(object sender, RoutedEventArgs e)
    {

    }
}

}

I think I've found a solution creating a new window for the dll user control in the menu exe as follows, something I thought was an automatic thing. If there is a better solution I appreciate letting me know. Thanks.

private void BtnRegEmpresa_Click(object sender, RoutedEventArgs e)
    {
        Window UserControlNewWindow = new Window
            {
            Title = "Some Title", Content = new UserControl1()
            };

        UserControlNewWindow.ShowDialog();
    }

One simple way:

in ur xaml of the Window add a ContentControl

as u click the menu put corresponding usercontrol in the contentcontrol

<ContentControl x:Name="CntUsercontrol"/>

In code behind

CntUserControl.Content = new UserControl();

If you use MVVM the you can switch the views based on the viewmodel using a DataTemplate.

Hope this helps

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