How to Invoke a Wpf Windows in a dll Class Library

Marino Liranzo 81 Reputation points
2023-01-13T03:02:24.1166667+00:00

Hello, I have added a Wpf window to a class library, now the problem is how to call it from a different project.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-01-13T07:40:57.6233333+00:00

    Hi, @Marino Liranzo . Welcome Microsoft Q&A.

    Is your class library Class Library (.NET Framework), Class Library or WPF Class Library? If it is WPF Class Library, you can refer to the following code. If there is any problem, please let me know.

    My Target Framework are both .NET 6.0.

    project structure:

    User's image

    You could right-click Denpencies under WPF Project (InvokeLibrary) and select Add Project References..., then Check WPF Class Library (WpfLibrary1).

    You can directly use the window in the class library in the wpf project.

    MainWindow.xaml:

     <Grid>
            <Button x:Name="btn" Content="show window1" Height="50" Click="btn_Click" />
        </Grid>
    
    

    MainWindow.xaml.cs:

      private void btn_Click(object sender, RoutedEventArgs e)
            {
                Window1 w = new Window1();
                w.Show();
            }
    
    

    The result:

    User's image


    If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.