简体   繁体   中英

Caliburn.Micro locking the main view

I am currently launching a rather lengthy operation from my main view. While this operation continues, it should be impossible to press any button on the main View. Furthermore, the progress should be reported in another Window. I could update every CanXXX Guard properties, but that would be tons of code.

I would like to use the ShowDialog Method of Caliburn.Micro, but this methods waits until the Window returns with a return value. So how can I show a modal Window with Caliburn.Micro which does not wait for some dialog to return ?

I've done this in the past...

In your main/shell view -

<ContentControl IsEnabled="{Binding UILocked, Converter={StaticResource BooleanInverter}}">
    <SomeOtherControls..... blah blah />
</ContentControl>

In the main/shell viewmodel -

public bool UILocked { get { // getter } set { // prop changed code/setter etc... } }

Upon setting UILocked to true the whole UI should be disabled. ContentControl will disable all child elements and IsEnabled bindings for its children will be ignored

You can ensure that the main view subscribes to the event aggregator too:

public class MainViewModel : IHandle<ChangeUIStateMessage>
{
    public bool UILocked { get { // getter } set { // prop changed code/setter etc... } }

    public MainViewModel(IEventAggregator aggregator)
    {
        aggregator.Subscribe(this);
    }

    public void Handle(ChangeUIStateMessage message)
    {
        UILocked = message.UILocked;
    }
}

The message class may be as simple as:

public class ChangeUIStateMessage
{
    public bool UILocked { get; private set; }

    public ChangeUIStateMessage(bool uiLocked)
    {
        UILocked = uiLocked;
    }
}

Then you can just fire this via the aggregator from any VM to lock/unlock the UI

aggregator.Publish(new ChangeUIStateMessage(true));

If needed you can also modify the handler so that a list of objects that have initiated a lock is maintained, ensuring the UI is locked until all workers have finished (in case multiple VMs kick off a lock/unlock operation simultaneously)

Edit: Forgot to add - if your popup controls are in a separate visual tree (as popups usually are) they will not be disabled which most of the time works to your advantage (interactive popups/locked app).

您可能要考虑扩展WPF工具包随附的BusyIndi​​cator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM