Example for using Caliburn Micro with m:ui?

May 7, 2013 at 3:05 PM
Hey,

can anyone share an example or tutorial on how to combine CM with m:ui?

Thanks
Coordinator
May 7, 2013 at 3:39 PM
Edited May 7, 2013 at 3:39 PM
This thread may help
May 7, 2013 at 3:48 PM
Thanks for pointing me to this thread, but I guess I still miss a starting point...
May 7, 2013 at 8:35 PM
Edited May 7, 2013 at 8:36 PM
Kozw, do you have a simple example to get me startend?
Coordinator
May 7, 2013 at 9:47 PM
I would love to provide you with a sample demonstrating Caliburn and mui, but my experience with caliburn micro is severely limited. This will take me some time to create.

Anyone care to chime in?
May 8, 2013 at 12:39 AM
Guys,

I just created a very, very, very simple sample app using "Modern UI for WPF" and "Caliburn Micro".
I'm new to WPF/MVVM world then do not expect too much on it, but it's something.

Link to sample git repository

Kozw, if you any idea of another sample please fell free to contact me.


Kind regards,

Gabriel Marquez
May 8, 2013 at 3:40 PM
Edited May 8, 2013 at 3:41 PM
Hey,

over at StackOverflow, someone (Charleh) answered my question providing some usefull (at least for me) stuff: StackOverflow

Regards,

Matthias
May 8, 2013 at 10:54 PM
Edited May 8, 2013 at 10:55 PM
Have you tried the example at StackOverflow and try to bind? I can get the views to load, but i cannot get the views and viewmodels to be bound when the view is loaded.

In CaliburnContentLoader::LoadContent() vm is always null.
May 8, 2013 at 11:10 PM
Edited May 8, 2013 at 11:28 PM
Yes, same problem here...Log shows (for View ExampleView): "View Model not found. Searched: ModernUIApp1, ModernUIApp1, ModernUIApp1.ExampleViewModel, ModernUIApp1.Example."
May 9, 2013 at 1:48 AM
The trick to "found" the ViewModel is override some methods and if needed add rule on ViewLocator.NameTransformer in the Caliburn Micro Bootstrap class.
On the sample provide here, it's all covered using MEF as IoC.
May 9, 2013 at 6:22 PM
gblmarquez that worked.
I am able to find the viewmodel and they are now bound. Based off the previous example, i just needed to add to my bootstrapper:
        protected override void Configure()
        {

            ViewLocator.NameTransformer.AddRule(
                @"(?<nsbefore>([A-Za-z_]\w*\.)*)?(?<nsvm>ViewModels\.)(?<nsafter>([A-Za-z_]\w*\.)*)(?<basename>[A-Za-z_]\w*)(?<suffix>ViewModel$)",
                @"${nsbefore}Views.${nsafter}${basename}View",
                @"(([A-Za-z_]\w*\.)*)?ViewModels\.([A-Za-z_]\w*\.)*[A-Za-z_]\w*ViewModel$"
                );

         ...
I use the simplecontainer so my code remaining is different from gblmarquez.

Thanks gblmarquez for figuring out that piece.

Now i have to figure out how to navigate from within a viewmodel.
May 10, 2013 at 12:20 AM
@ricorx7 I'm working on navigation right now, I'm not satisfied with my solution.
If you find something interesting please share :D thanks.

Kind regards,

Gabriel Marquez
May 10, 2013 at 10:02 PM
Edited May 10, 2013 at 10:17 PM
If i use the standard MenuLinksGroup, the navigation works fine. The ContentLoader finds the ViewModel based off the View path given and the ModernWindow navigates to the correct view and binding works fine.

But i am also trying to navigate by a button click. And this does not work.

So in ShellViewModel i have added this:
        private void NavigateToView(string view)
        {
            var frame = (ShellView) ViewLocator.LocateForModelType(typeof(ShellViewModel), null, null);
            if (frame != null)
            {
                frame.ContentSource = new Uri(view, UriKind.Relative);
            }
        }
And when i run the method:
NavigateToView("/Views/Page1.xaml");
i would expect the ContentLoader to be called and the frame to navigate to the page. But none of this happens.
The constructor for Pag1's view or viewmodel is not called. The ContentLoader is not called.

Am i suppose to do something after ContentSource is set? In all the examples, Source is set, but i cannot find this property for the ModernWindow.

I also have not gotten the IContent interface to work. None of the Navigate methods are called when i navigate using the MenuLinksGroup links.
May 11, 2013 at 1:18 AM
Edited May 11, 2013 at 1:27 AM
Ok so i found what i was missing, a ModernFrame, but when i incorporate the ModernFrame somewhere i still do not get navigation to work. But i believe i am getting closer.

The Shell is a ModernWindow. The Shell's ContentSource is a ModernFrame called mainFrame. I made mainFrame's ContentLoader be my CaliburnContentLoader. Then i made mainFrame's Source HomeView which is a UserControl (this is a hack until i figure out how to load mainFrame somewhere properly). The HomeView is loaded at startup properly. But navigation does not work from a button in code. It does work from a button using xaml.


WORKS
Command="NavigationCommands.GoToPage" CommandParameter="/Views/Page1View.xaml"
DOES NOT WORK
var frame = NavigationHelper.FindFrame(null, Shell);
frame.Source = new Uri("/Views/Page1View.xaml", UriKind.Relative);
Now i thought that because Shell had mainFrame as it's ContentSource i could use Shell to find the frame.
But no frame is found. I found the code for NavigationHelper and it is looking for the Decendents of Shell that are ModernFrames which it finds none.

So i cheated and put mainFrame in my container so at least i could find it and then set the Source for mainFrame, but the view never navigates.

I followed how NavigationCommands.GoToPage does it of setting the Source to a URI of the page path, so i am not sure why it does not work.
May 14, 2013 at 10:57 PM
@ricorx7, Nice. I'll create a lab to test this approach!

Thanks for sharing