Share via

Add and run a WPF project to an existing solution

yaron kadman 81 Reputation points
Oct 12, 2020, 12:58 PM

Hi,
(VISUAL STUDIO 2019)
I have a VSTO project (I think the question can be for Winforms projects as well).
I have a WPF project that I want to interact with from the VSTO project and back.

I managed to run (process.start with parameters) the the WPF exe from the VSTO but it's not good enough because I want to be able to communicate between the two projects.

I added the WPF project to the VSTO solution, and added a reference from the VSTO to the WPF and then create a "common project" that both projects reference to be able to pass data in common models.

I am able to run the WPF project from the VSTO (when the user clicks a button, in the UI of the VSTO, I call App.Main() in the WPF project).

The problem is that if I try to use App.Main() again (to call the WPF project again) I get an exception because I can not create a second Application in the appdomain.

Any thoughts on how I can accomplish this (my way or any other way that will let me interact between the two projects).

Thank you

Yaron

I tried different ways to

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,805 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,631 Reputation points
    Oct 13, 2020, 6:46 AM

    I create a WordDocument project named WordDocumentTest in VSTO follow the steps in Create the project and a WPF project named WpfAppRunInVSTO.

    Step 1: Add WpfAppRunInVSTO as reference in WordDocumentTest

    Step 2: Add below references: PresentationCore,PresentationFramework,WindowsBase to WordDocumentTest

    Step 3: Add a button in the Document with a Click event

    Step 4: The code for ThisDocument

    public partial class ThisDocument  
        {  
            private void ThisDocument_Startup(object sender, System.EventArgs e)  
            {  
                this.Paragraphs[2].Range.InsertParagraphAfter();  
                this.Paragraphs[3].Range.Text = "This text was added by using code.";  
                this.Paragraphs[3].Range.Bold = 50;  
      
                this.Paragraphs[3].Range.InsertParagraphAfter();  
      
                this.Paragraphs[4].Range.InsertDateTime();  
      
            }  
      
            private void ThisDocument_Shutdown(object sender, System.EventArgs e)  
            {  
            }  
      
            #region VSTO Designer generated code  
            private void InternalStartup()  
            {  
                this.button1.Click += new System.EventHandler(this.button1_Click);  
                this.Startup += new System.EventHandler(this.ThisDocument_Startup);  
                this.Shutdown += new System.EventHandler(this.ThisDocument_Shutdown);  
      
            }  
      
            #endregion  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                var t = new Thread(() =>  
                {  
                    MainWindow window = new MainWindow();  
                    window.Closed += (s2, e2) => window.Dispatcher.InvokeShutdown();  
                    window.Show();  
                    System.Windows.Threading.Dispatcher.Run();  
                });  
      
                t.SetApartmentState(ApartmentState.STA);  
                t.Start();  
               
            }  
    

    The result picture as below shown:
    31916-2.gif


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


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.