RadControls version
|
Silverlight Q1 2011
|
.NET version |
4
|
Visual Studio version |
2010
|
programming language |
C#
|
browser support |
all browsers supported by RadControls
|
PROJECT DESCRIPTION
A small project that shows how to extend the Caliburn Micro framework to enable the use of a RadWindow as dialog in Caliburn Micro. Be sure to familiarise yourself with the Caliburn.Micro.HelloWindowManager sample project first.
I started by extending the IWindowManager manager by adding a method for calling RadWindows:
Then I added an implementation of the method in the WindowManager class:
The EnsureRadWindow method implementation:
Finally, the RadWindowConductor class that handles the closing and deactivating:
public
class
RadWindowConductor
{
bool
deactivatingFromView;
bool
deactivateFromViewModel;
bool
actuallyClosing;
readonly
RadWindow view;
readonly
object
model;
public
RadWindowConductor(
object
model, RadWindow view)
{
this
.model = model;
this
.view = view;
var activatable = model
as
IActivate;
if
(activatable !=
null
)
activatable.Activate();
var deactivatable = model
as
IDeactivate;
if
(deactivatable !=
null
)
{
view.Closed += Closed;
deactivatable.Deactivated += Deactivated;
}
var guard = model
as
IGuardClose;
if
(guard !=
null
)
view.PreviewClosed += PreviewClosed;
}
void
PreviewClosed(
object
sender, WindowPreviewClosedEventArgs e)
{
if
(e.Cancel !=
null
&& (
bool
)e.Cancel)
return
;
var guard = (IGuardClose)model;
if
(actuallyClosing)
{
actuallyClosing =
false
;
return
;
}
bool
runningAsync =
false
, shouldEnd =
false
;
guard.CanClose(canClose =>
{
Execute.OnUIThread(() =>
{
if
(runningAsync && canClose)
{
actuallyClosing =
true
;
view.Close();
}
else
e.Cancel = !canClose;
shouldEnd =
true
;
});
});
if
(shouldEnd)
return
;
e.Cancel =
true
;
runningAsync = (
bool
)e.Cancel;
}
void
Closed(
object
sender, EventArgs e)
{
view.Closed -= Closed;
if
(deactivateFromViewModel)
return
;
var deactivatable = (IDeactivate)model;
deactivatingFromView =
true
;
deactivatable.Deactivate(
true
);
deactivatingFromView =
false
;
}
void
Deactivated(
object
sender, DeactivationEventArgs e)
{
if
(!e.WasClosed)
return
;
((IDeactivate)model).Deactivated -= Deactivated;
if
(deactivatingFromView)
return
;
deactivateFromViewModel =
true
;
actuallyClosing =
true
;
view.Close();
actuallyClosing =
false
;
deactivateFromViewModel =
false
;
}
}
In order to use it, you will need a
RadWindow used as a User Control with an associated ViewModel as per the Caliburn Micro conventions. You will need to use the WindowManager instance obtained from the container in the bootstrapper to perform the action:
Example usage:
Enjoy
Stefan Buys