Caliburn Micro and ModernUI Examples/Tutorials
Asked Answered
C

2

17

does anyone have an example or tutorial on how to use Caliburn Micro together with ModernUi (https://mui.codeplex.com)?

Conakry answered 7/5, 2013 at 14:27 Comment(2)
I'd imagine since ModernUI looks like a collection of controls that you would just need to add the conventions for each control in the toolkit. The only difference would be that CM uses ChildWindow for most of it's window management, and you'd probably want to replace this with ModernWindow in your implementation. I think you would probably only need to provide your own implementation for WindowManager (and specifically EnsureWindow method) caliburnmicro.codeplex.com/SourceControl/changeset/view/…Jeconiah
Well, after having a look, it looks to be more complex than that. I think that providing your own WindowManager implementation might not be the best idea since all popups would also implement the ModernWindow class. Also it looks like it loads content dynamically based on resource URLs and therefore a viewmodel-first approach would probably not work.Jeconiah
J
21

Ok so I had a quick mess about with it and a look on the Mui forums and this seems to be the best approach:

Since the window loads content from URLs you need to take a view-first approach, and then locate the appropriate VM and bind the two.

The best way to do this appears to be via the ContentLoader class which is used to load the content into the ModernWindow when it is requested. You can just subclass DefaultContentLoader and provide the necessary CM magic to bind up loaded items:

public class ModernContentLoader : DefaultContentLoader
{
    protected override object LoadContent(Uri uri)
    {
        var content = base.LoadContent(uri);

        if (content == null)
            return null;

        // Locate the right viewmodel for this view
        var vm = Caliburn.Micro.ViewModelLocator.LocateForView(content);

        if (vm == null)
            return content;

        // Bind it up with CM magic
        if (content is DependencyObject)
        {
            Caliburn.Micro.ViewModelBinder.Bind(vm, content as DependencyObject, null);
        }

        return content;
    }
}

Your CM bootstrapper should just bootstrap a ModernWindow viewmodel which is backed by a ModernWindow based view (CM tries to use EnsureWindow which creates a new basic WPF Window class, unless of course your control already inherits from Window which ModernWindow does. If you need all dialogs and popups to be MUI you might need to reimplement WindowManager):

public class Bootstrapper : Bootstrapper<ModernWindowViewModel>
{
}

Which can be a conductor (OneActive) and looks like this:

public class ModernWindowViewModel : Conductor<IScreen>.Collection.OneActive
{
}

And XAML for the view is

ModernWindowView.xaml

<mui:ModernWindow x:Class="WpfApplication4.ViewModels.ModernWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mui="http://firstfloorsoftware.com/ModernUI"
                                     Title="ModernWindowView" Height="300" Width="300" ContentLoader="{StaticResource ModernContentLoader}">   
    <mui:ModernWindow.MenuLinkGroups>
        <mui:LinkGroupCollection>
            <mui:LinkGroup GroupName="Hello" DisplayName="Hello">
                <mui:LinkGroup.Links>
                    <mui:Link Source="/ViewModels/ChildView.xaml" DisplayName="Click me"></mui:Link>
                </mui:LinkGroup.Links>
            </mui:LinkGroup>
        </mui:LinkGroupCollection>
    </mui:ModernWindow.MenuLinkGroups>
</mui:ModernWindow>

Obviously you need to make the loader a resource too:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
            <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Dark.xaml"/>
            <ResourceDictionary>
                <framework:ModernContentLoader x:Key="ModernContentLoader"></framework:ModernContentLoader>
                <wpfApplication4:Bootstrapper x:Key="Bootstrapper"></wpfApplication4:Bootstrapper>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Here's the ChildViewModel I'm using as a test:

public class ChildViewModel : Conductor<IScreen>
{
    public void ClickMe()
    {
        MessageBox.Show("Hello");
    }
}

And the XAML for that (just a button)

<UserControl x:Class="WpfApplication4.ViewModels.ChildView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                     Height="350" Width="525">
    <Grid>
        <StackPanel>
        <TextBlock >Hello World</TextBlock>
        <Button x:Name="ClickMe" Width="140" Height="50">Hello World</Button>
        </StackPanel>
    </Grid>
</UserControl>

And the proof of concept:

MUI Example

Jeconiah answered 8/5, 2013 at 11:24 Comment(15)
Hey Charleh, thanks again for your answer. There is one problem left: In the ContentLoader, when the ViewLocator looks for the matching viewmodel, it always returns null and no Binding ist done. Any Idea?Conakry
Charleh, could you upload your working sample source somewhere?Conakry
Yeah I'll do it tomorrow it's sleep time now! Mine resolves the vm correctly, might be something to do with your namespaces, what namespace did you put your view/vm in and what did you call them both?Jeconiah
I've uploaded it to filedropper.com/wpfapplication4 - you will probably need to NuGet the packages as I've removed them and I'm not sure if NuGet will get them automatically based on packages.config in the solution dir...I used Castle.Windsor for IoCJeconiah
Ah apparently you can use nuget install packages.configJeconiah
Argh - but create a packages directory in the solution dir first and copy packages.config from the project directory into that, and run it from there, otherwise it dumps them all in your project dir!Jeconiah
Charleh, thank you very much. Must have been some issues with Castle. Now it just works.Conakry
More info about NuGet here that I didn't know (if it helps) #6877232 - didn't know you could just do package restore in VSJeconiah
According to @AlexJustin, the link with the uploaded application is dead. He has requested that you reupload it if you can to a new host?Philbrook
Try it again, I've just reuploaded it to the same placeJeconiah
@Charleh, great instruction, thanks! But I can't figure out how to open new popup windows using this combination. I tried to modify WindowManager as shown here but had no success. Can you help maybe?Taratarabar
As I get it to achieve the goal I need to import existing WindowManager to ChildViewModel somehow, and then use it to open new window.Taratarabar
Hi ValeO, do you mind asking this as a new question on SO? That way I can help out without polluting this existing question. Bottom line is, you will probably need to re-implement WindowManager and register your own window manager as the implementation for the IWindowManager service. This WindowManager will use MordernWindow instead of the default windows.Jeconiah
Charleh, take a look here please. Definitely need your help.Taratarabar
Hi Charleh, can you upload your application once more time. I have the same problem with null reference in ContentLoader. Thanks in advice.Shillyshally
C
10

I create a very, very simple sample of chat app using Modern UI for WPF, Caliburn Micro and MEF.

https://github.com/gblmarquez/mui-sample-chat

I hope it helps

Caponize answered 9/5, 2013 at 0:50 Comment(2)
How do i navigate using commands? i am getting an error: when i use Command="mui:LinkCommands.NavigateLink". The error is "LinkCommands is not supported in a Windows Presentation Foundation (WPF) project."Lasandralasater
Even BBCodeBlock URL is not showing the URLLasandralasater

© 2022 - 2024 — McMap. All rights reserved.