Caliburn.Micro Not Handle To ViewModel

Nov 4, 2012 at 7:15 PM

Hi,

I'm try Caliburn.Micro, click button in TestView not show messagebox in TestViewModel, how to solved this problem, Project Attached.

Thanks for very nice framework.

Direct Download

http://bayfiles.com/file/qjDR/8h3rCB/Test.zip

Nov 4, 2012 at 7:38 PM

not sure what you are trying to get folks to down load, but Test.zip from that link is not it... either repost with a properly link or quit messing with the folks here.

Nov 5, 2012 at 5:07 AM
Edited Nov 5, 2012 at 5:58 AM

Hi,

Thanks for reply,

 Bootstrapper.cs

namespace Test
{
    public class Bootstrapper : Bootstrapper<IShell> {
        private CompositionContainer container;

        protected override void Configure() {
            container = new CompositionContainer(
                new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)))
                );

            var batch = new CompositionBatch();

            batch.AddExportedValue<IWindowManager>(new WindowManager());
            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue(container);

            container.Compose(batch);
        }

        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = container.GetExportedValues<object>(contract);

            if (exports.Count() > 0)
                return exports.First();

            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }

        protected override void BuildUp(object instance)
        {
            container.SatisfyImportsOnce(instance);
        }
    }
}
IShell.cs
namespace Test
{
    public interface IShell
    {

    }

}
App.xaml
 
<Application x:Class="Test.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Test">
    
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
 
MainWindowView.xaml
<Window x:Class="Test.Views.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Views="clr-namespace:Test.Views" 
        Title="Test" Height="300" Width="600" WindowStartupLocation="CenterScreen" >
   
        <Grid>
            <TabControl>
                <TabItem Header="TabItem1" IsSelected="True">
                    <Grid>
                        <Views:TestView />
                        
                    </Grid>
                </TabItem>
            </TabControl>
        </Grid>
</Window>
MainWindowViewModel.cs
namespace Test.ViewModels
{
    [Export(typeof(IShell))]
    public class MainWindowViewModel : Screen, IShell {
        readonly IWindowManager _windowManager;


        [ImportingConstructor]
        public MainWindowViewModel(IWindowManager windowManager)
        {
            this._windowManager = windowManager;
        }

    }
}
TestView.xaml
<UserControl x:Class="Test.Views.TestView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <Button x:Name="ShowMess" Content="Button" HorizontalAlignment="Left" Height="41" Margin="141,124,0,0" VerticalAlignment="Top" Width="123"/>

    </Grid>
</UserControl>

TestViewModel.cs
namespace Test.ViewModels
{
    [Export(typeof(IShell))]
    public class TestViewModel : Screen
    {
        readonly IWindowManager _windowManager;

        [ImportingConstructor]
        public TestViewModel(IWindowManager windowManager)
        {
            this._windowManager = windowManager;

        }

        public void ShowMess()
        {
            MessageBox.Show("Text", "Caption", MessageBoxButton.YesNo);

        }
    }
}
Not Show MessageBox if clicked ShowMess Button.
Thanks so much.
Nov 5, 2012 at 7:10 AM
Edited Nov 5, 2012 at 7:11 AM

You're going from ViewModel first to View-First with the way you placed the TestView on the tabcontrol... So the TestViewModel for TestView isn't bound to the TestView,  I am gonna bet if you run the Logger to output to the Debug on Output window it would show this.

Nov 5, 2012 at 7:59 AM

Hi,

Thanks for reply, Now what is the solution I am newbie, Please help me.

Nov 5, 2012 at 9:00 AM

You could try to add on the TestView the cal:Bind,Model="Test" as an attribute of the UserControl and it should be bound to the TestViewModel

<UserControl
             cal:Bind.Model="Test" />

 

Nov 5, 2012 at 10:29 AM
Edited Nov 5, 2012 at 11:18 AM

Thanks so Much Solved My Problem :x