我正在学习Ninject和Caliburn,并且正在测试和阅读.....
在我的示例中,我找到了实现IResolutionRoot的方法,
但是我已经阅读过使用Factory的可能性吗?
我已经阅读了有关Ninject.Extensions.Factory(代理)的Wiki,但我看不到示例中的实现方法,您能帮助我展示实现此方法的正确方法吗?我公开了可以帮助初学者的源代码。
第二个问题,是使用caliburn(具有静态功能)将视图和视图模型绑定的正确代码吗?
这是我的引导者
using Caliburn.Micro;
using Ninject;
using System;
using System.Collections.Generic;
using System.Windows;
using TestNinjectCaliburn.ViewModels;
using EventAggregator = TestNinjectCaliburn.Events.EventAggregator;
using IEventAggregator = TestNinjectCaliburn.Events.IEventAggregator;
namespace TestNinjectCaliburn
{
public class Bootstrapper : BootstrapperBase
{
private IKernel kernel;
public Bootstrapper()
{
Initialize();
}
protected override void Configure()
{
kernel = new StandardKernel();
kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
kernel.Bind<MainWindowViewModel>().ToSelf().InSingletonScope();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<MainWindowViewModel>();
}
protected override object GetInstance(Type service, string key)
{
return kernel.Get(service);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return kernel.GetAll(service);
}
}
}
我的SecondView构建类的实例:
using Caliburn.Micro;
using Ninject;
using Ninject.Parameters;
using Ninject.Syntax;
using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
namespace TestNinjectCaliburn.Views
{
public partial class SecondView : Window
{
private readonly IResolutionRoot resolutionroot;
public SecondView( IResolutionRoot resolutionroot)
{
this.resolutionroot = resolutionroot;
InitializeComponent();
Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "TestNinjectCaliburn.Gauges");
var left = 0d;
var top = 100d;
var id = 0;
foreach(Type t in typelist)
{
string dis = $"{((char)(id + 'A')).ToString()}";
//Parameter for constructors VievModel A, B and C
Ninject.Parameters.Parameter[] param = new Ninject.Parameters.Parameter[] { new ConstructorArgument("id", dis, true) };
// Replace the Activator.CreateInstance
var viewmodel = resolutionroot.TryGet(t, param);
//binding view and viewmodel for Caliburn
var view = ViewLocator.LocateForModel(viewmodel, null, null);
ViewModelBinder.Bind(viewmodel, view, null);
MainGrid.Children.Add(view);
var uc = view as UserControl;
uc.Margin = new Thickness(left, top, 0, 0);
id = id + 1;
left = left + 150;
}
}
private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return
assembly.GetTypes()
.Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal) && t.FullName.EndsWith("ViewModel"))
.ToArray();
}
}
}
SecondView Xaml:
<Window x:Class="TestNinjectCaliburn.Views.SecondView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestNinjectCaliburn.Views"
mc:Ignorable="d"
Title="SecondView" Height="400" Width="800">
<Grid x:Name="MainGrid">
</Grid>
</Window>
一个调用viewModel及其相关视图的示例:
using Caliburn.Micro;
using IEventAggregator = TestNinjectCaliburn.Events.IEventAggregator;
namespace TestNinjectCaliburn.Gauges
{
public class A_ViewModel:PropertyChangedBase
{
private readonly IEventAggregator eventAggregator;
public A_ViewModel(IEventAggregator eventAggregator, string id)
{
this.eventAggregator = eventAggregator;
Display = id;
}
private string display;
public string Display
{
get { return display; }
set
{
display = value;
NotifyOfPropertyChange(() => Display);
}
}
public void Test()
{
Display = "I am A";
}
}
}
其视图:
<UserControl x:Class="TestNinjectCaliburn.Gauges.A_View"
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"
xmlns:local="clr-namespace:TestNinjectCaliburn.Gauges"
mc:Ignorable="d"
d:DesignHeight="110" d:DesignWidth="100">
<Grid>
<Button x:Name="Test" Content="Test Caliburn" Margin="0,0,0,0" Width="100" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBlock x:Name="Display" Margin="0,40,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="30"/>
</Grid>
</UserControl>
感谢您的帮助