locked
Getting Exception When Trying to Open a Custom Modal Window RRS feed

  • Question

  • Hi Everybody

    I am trying to open a custom Add/Edit modal window from Reusable Modal Screen (Demonstrated by Jan Van der Haegen). But unfortunately getting the below error (After pressing on "+" icon from reusable modal screen).

    IVisualCollection<T>.AddNew() should not be called from UI Thread.

    The Code I am Using

    partial void gridAddAndEditNew_Execute()
            {
                // Write your code here.
                Dispatchers.Main.BeginInvoke(() =>
                {
                    this.Groups.AddNew();
                    this.OpenModalWindow("GroupAddEditModalWindow");
                    this.FindControl("GroupAddEditModalWindow").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
                    this.FindControl("GroupAddEditModalWindow").DisplayName = "Add New Group";
                });
               
                Dispatchers.Main.BeginInvoke(() =>
                {
                    this.FindControl("CancelGroup").IsVisible = true;
                    this.FindControl("CancelEdit").IsVisible = false;
                });
            }

    Please find the below image to know the error in detail

    Any help will be highly appriciated

    Thanks


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Wednesday, May 1, 2013 3:21 AM

All replies

  • Do exactly as the error message tells you to: don't call that API from the UI thread (also known as the main thread). Not sure why you are dispatching to the main thread, since those APIs are supposed to be used from the logic thread.

    Justin Anderson, LightSwitch Development Team

    Wednesday, May 1, 2013 3:59 AM
    Moderator
  • Hi Justin

    Thank you for your reply.

    I thought there might be some threading issue when calling the modal window. If I remove the Main dispatcher I got the following exception.

    Screen is already closed

    It will be better if you can suggest me the exact solution on how to get rid off this error. As per my application workflow I can't modify the UI. This UI is perfectly suit my application workflow.

    Thanks


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Wednesday, May 1, 2013 4:18 AM
  • That means that the screen was already closed when the method is being executed. Could almost be anything that causes this depending on what code you wrote.

    Some examples:

    • If you are holding a reference to this screen from another part of the application (e.g. another screen) and try to invoke an API on the screen after it's closed, this problem will occur.
    • Another common occurrence is when developers implement a timer to refresh the screen, which actually causes a new instance to be created, but they forgot to shutdown the timer on the old instance.

    Justin Anderson, LightSwitch Development Team

    Wednesday, May 1, 2013 5:37 AM
    Moderator
  •  

    Hey guys,

    the "reusable modal dialog" post describes a process that takes an IScreenObject (= "model"), then takes the corresponding View, hijacks the rootVisual and re-hosts that inside a custom popup dialog.

    It works perfectly for a lot of "simple" scenarios, but once you call pop ups from the popup, things seem to go wrong.

    Here's my guess, from your command handler you are calling code on "this" (this.OpenModal...).  "This" is the IScreenObject implementation, and once you call code on it it probably defensively checks it's own state before actually executing that code.  It "incorrectly" throws an error because the screen itself believes it's not open ("screen already closed").  
    My guess is that this is NOT because the screen is actually closed (like the error message says), but because no one actually updated the screen object so that it knows that it is open... (ie: because the normal "LightSwitch" infrastructure probably does this, but our hack doesn't).
    Maybe Justin can share some insights on the internals here, but I'd understand if this is MS proprietary information...

    Assuming this is the problem, let's try to find a solution.  The walkthrough on creating a custom Shell (http://msdn.microsoft.com/en-us/library/vstudio/hh290138.aspx) does exactly the same thing as I proposed: grab the IScreenObject, ask for the corresponding View, and re-host the rootVisual in an item tab.  However, the custom shell example also sets the "CurrentScreen" property on the activeScreensViewModel, for rather undocumented reasons.  (ie: who knows why?)
    Let's test if this is the missing link, that fixes the internal state of the model.  Just before your code, you could write something hacky to mimic that, like         VsExportProviderService.GetExportedValue<IServiceProxy>().ActiveScreensViewModel.Current = this:
    Although that's obviously not the clean and structural fix, it can help us discover if we're on the right track or not...  Best of luck!

    Keep rocking LS!

    Jan


    It's your story - time to switch on the innovation. || About me || LightSwitch blog

    Monday, May 6, 2013 9:29 AM
  • I'm getting the same thing...but the funny thing for me is that the very first time, the popup worked.

    After that, I always get the "Screen is already closed" message.  I thought maybe it's cached somewhere, but killings IIS Express, restart VS, restart computer...the popup never work again.

    So, why did it worked the very first time?

    Wednesday, May 15, 2013 5:06 PM