Wpf 结合MahApps.Metro和Caliburn.Micro
嗯,我尝试使用MahApps.Metro和Caliburn.Micro,但我遇到了一些问题 这是我的引导器Wpf 结合MahApps.Metro和Caliburn.Micro,wpf,caliburn.micro,mahapps.metro,Wpf,Caliburn.micro,Mahapps.metro,嗯,我尝试使用MahApps.Metro和Caliburn.Micro,但我遇到了一些问题 这是我的引导器 public sealed class TestBootstrapper : Bootstrapper<ShellViewModel> { private CompositionContainer container; protected override void Configure() { container = new Comp
public sealed class TestBootstrapper : Bootstrapper<ShellViewModel>
{
private CompositionContainer container;
protected override void Configure()
{
container = new CompositionContainer(new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
CompositionBatch batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new AppWindowManager());
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();
}
return base.GetInstance(serviceType, key);
}
}
这在某种程度上是可行的,但我的窗口周围有一个黑色边框,直到我重新调整大小时才看到下图
为什么会有黑色边框?我如何才能消除它(如果我手动调整窗口大小,边框就会消失。)?似乎SizeToContent是问题所在 该问题的解决方案是将ResizeMode属性设置为“NoResize”,然后正确渲染边框
window = new MetroWindow()
{
Content = view,
SizeToContent = SizeToContent.WidthAndHeight,
ResizeMode = ResizeMode.NoResize
};
对于Caliburn.Micro 2和Mahapps.Metro 1,上述内容不再是这两个框架的有效组合 在对卡利本的一些微文档和过时的指南进行挖掘之后,我得出了以下结论 首先创建引导程序,如下所示:
public class AppBootstrapper : BootstrapperBase
{
private CompositionContainer container;
public AppBootstrapper()
{
Initialize();
}
protected override void Configure()
{
container = new CompositionContainer(
new AggregateCatalog(
AssemblySource.Instance.Select(
x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
)
);
CompositionBatch batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
}
protected override object GetInstance(Type service, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;
var exports = container.GetExportedValues<object>(contract);
if(exports.Any())
{
return exports.First();
}
throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetExportedValues<object>(AttributedModelServices.GetContractName(service));
}
protected override void BuildUp(object instance)
{
container.SatisfyImportsOnce(instance);
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<IShell>();
}
}
最后但并非最不重要的一点是,我们需要设置ShellView以使用MahApps.Metro窗口:
<Controls:MetroWindow x:Class="your-namespace.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
<Grid Background="White">
<TextBlock Text="Hello Caliburn Micro!"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="20" />
</Grid>
</Controls:MetroWindow>
希望这能帮助其他希望在最新迭代中结合Caliburn.Micro和MahApps.Metro的人。也许你应该将其标记为答案,如果相关的话,也许可以添加更多信息?对于那些感兴趣的人,似乎只需删除“SizeToContent=SizeToContent.WidthAndHeight”就可以解决问题谢谢你的发布,你上面的代码为我节省了不少时间!如果有人可以验证这是正确的解决方案,那么我会将其标记为答案!Phil,谢谢你发布这篇文章,这对我使用Caliburn和Mahapps Metro的最新版本很有效。你的文章似乎没有给出任何关于如何创建MetroWindow的新窗口的指示。Caliburn的默认WindowManager总是创建窗口,而不是MetroWindows。你有解决这个问题的办法吗?
<Application x:Class="your-namespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:your-namespace">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:AppBootstrapper x:Key="bootstrapper" />
</ResourceDictionary>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
}
<Controls:MetroWindow x:Class="your-namespace.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
<Grid Background="White">
<TextBlock Text="Hello Caliburn Micro!"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="20" />
</Grid>
</Controls:MetroWindow>