Quantcast
Channel: My musings on .Net Technology » Uncategorized
Viewing all articles
Browse latest Browse all 17

Using Popup with Caliburn Micro

$
0
0

If you use Caliburn Micro to show Popup window such as below:

ShowPopup

Then you might have noticed that the popups do not auto-close, second you cannot control the popup window and set “IsOpen=false” or “StaysOpen=false” property, this is because CM’s window manager creates the popup window and our VM does not have reference to it. We cannot use CM screen conductor as Popups are new views and not in place replacement or ActiveView.

Therefore with the default invocation, you may see artifacts such as below on invoking multiple popups (in our example we show popup on textbox right click)
popupsgalore

The core xaml for the above view is
xaml

One way to deal with this issue is to implement the CM’s IViewAware interface on our popup view model (which in our example is the CustomPopupViewModel). The reason you want to implement IViewAware is that it makes the viewmodel aware of the view, allowing the view model to get direct reference to view elements and control. The way CM achieves this is: When CM instantiates the view (instantiate, and not initialized), it checks to see if there is any IViewAware interface on the respective view-model, if so, it passes the reference of the view to the view-model before it initializes the view; view model can then set the required attributes etc before the view is initialized.

The IViewAware interface makes the view-model view aware (which is not a good practice per the MVVM pattern, but in cases such as popup it is the required approach). With that explained, our view model would look something as below, where the view can set the required attributes thus removing the artifacts.

custompopup



Viewing all articles
Browse latest Browse all 17

Trending Articles