Caliburn Micro and ModernUI Examples/Tutorials

前端 未结 2 936
遇见更好的自我
遇见更好的自我 2020-12-13 21:19

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

2条回答
  •  天涯浪人
    2020-12-13 21:36

    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
    {
    }
    

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

    public class ModernWindowViewModel : Conductor.Collection.OneActive
    {
    }
    

    And XAML for the view is

    ModernWindowView.xaml

       
        
            
                
                    
                        
                    
                
            
        
    
    

    Obviously you need to make the loader a resource too:

    
        
            
                
                
                
                    
                    
                
            
        
    
    

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

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

    And the XAML for that (just a button)

    
        
            
            Hello World
            
            
        
    
    

    And the proof of concept:

    MUI Example

提交回复
热议问题