Open/Close wpf window

Open/Close wpf window

micle.space
Enthusiast Enthusiast
2,757 Views
5 Replies
Message 1 of 6

Open/Close wpf window

micle.space
Enthusiast
Enthusiast

Dear all,

I'm developing a wpf application with a collection of methods in order to handle my dwg files.

I have to tell you the  true, I'm quite satisfied, my App is reaching really a good level and now I can do in minutes what I did in days... For these reasons I can only thank this Forum and all of you are helping me.

Now, let's go to the matter. All works well, but if I open and close my method in the same Autocad session something goes wrong and I have to close the Autocad through win10 task manager. 

My suspicious  are going to the way I open and close my wpf window. Here my opening method:

 

 

        [CommandMethod("MYACADWPF", CommandFlags.Modal)]
        public void MyACADUtilitiesWPF() 
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed;
            if (doc != null)
            {
                ed = doc.Editor;
                MyAcadWinGUI acadWinGui = new MyAcadWinGUI();
                //AcAp.ShowModalDialog(acadWinGui);
                acadWinGui.Show();

            }
        }

 

 

And for closing I just use the Close cross in the app's title window. 

Have I to catch the closing event and dispose something?  

All my methods are in Using statement (transaction and database), all dwg files are opend as OpenSideDatabase. and all tasks are in background.

 

Thanks to all

M.

0 Likes
Accepted solutions (2)
2,758 Views
5 Replies
Replies (5)
Message 2 of 6

marcin.sachs
Advocate
Advocate
Accepted solution

Hi,
I'm using ShowModalWindow() and I never noticed that it was closed incorrectly.

var dialog = new RevisnionNew();
var result = Application.ShowModalWindow(dialog);

of course you need defined your wpf as window:

<Window x:Class="Windows.RevisionsManager.RevisnionNew"
             ...
</Window>
Message 3 of 6

norman.yuan
Mentor
Mentor
Accepted solution

I am not sure why do you regard your code as "All works well", because when running the exact code of yours (only with an empty WPF window, or Win Form window, for that matter), the UI/window would be a MODELESS window, but it DOES NOT float on top of AutoCAD window (that is, after the UI shows, if user clicks anywhere in AutoCAD, the UI would be hidden behind AutoCAD, which is NOT how a floating AutoCAD window should behave. Furthermore, with your command, if user somehow execute the command multiple times without closing the UI (because the UI is modeless, user can do that), AutoCAD would end up showing the UI multiple times.

 

I do notice you have following line of code commented out:

//AcAp.ShowModalDialog(acadWinGui);

 Actually, that is the correct way to show UI in AutoCAD. Of course, since the UI is WPF, you should call ShowModal[Modeless]Window().

 

So, you should ALWAYS use

 

Application.ShowModal[Modeless]Dialog[Window]()

 

to show your UI, instead of Form[Window].Show()/ShowDialog()

 

Also, if you want to show UI as modeless (floating) UI, you want to pay extra attention, because while your UI is showing, user could change AutoCAD's active document. In this case, you would either want to make the modeless UI singleton instance and update displaying content when active document changes, or per-document instance and hide/show them while active document changes.

 

With all said, the AutoCAD freezing you described may not may not be directly related to the wrong way your code shows the UI. Your mentioning of "... all tasks are in background..." sounds a bit suspicious to me.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 6

norman.yuan
Mentor
Mentor

Sorry, did not see your next post before posting my reply. Now that you are using ShowModalWindow(), then the showing UI is surely not the cause of AutoCAD freezing. So, please explain "all tasks are in background".

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 6

micle.space
Enthusiast
Enthusiast

Thanks Marcinsasch and Norman for your replay and taking care of my case, in the while I was writing my initial post I was going to do some experiments and I was investigating on showmodaldialog (this is why was commented) my actual code was:

 

 

 

        [CommandMethod("MYACADWPF")]
        public void MyACADUtilitiesWPF() 
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed;
            if (doc != null)
            {
                ed = doc.Editor;
                MyAcadWinGUI acadWinGui = new MyAcadWinGUI();
                acadWinGui.Show();
            }
        }

 

 

 

so summarizing I will add commandFlag.Modal in commandMethod and I will open my WPF-GUI through ShowModalDialog. I will test it soon. 

Answering to Norman, with "all task are in background" I mean all my methods do not require user input from autocad or from active drawings and if the user (me...) opens other drawings doesn't effect the running of my method.

Cheers

M.

0 Likes
Message 6 of 6

micle.space
Enthusiast
Enthusiast

One small step for a man, one giant leap for mankind...

0 Likes