wpf 结合 MahApps.Metro 和 Caliburn.Micro
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12045347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining MahApps.Metro and Caliburn.Micro
提问by Peter
Well im trying to use MahApps.Metro and Caliburn.Micro together but im getting some problems
好吧,我试图将 MahApps.Metro 和 Caliburn.Micro 一起使用,但我遇到了一些问题
Here is my bootstrapper
这是我的引导程序
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);
}
}
And here is my AppWindowManager
这是我的 AppWindowManager
public sealed class AppWindowManager : WindowManager
{
static readonly ResourceDictionary[] resources;
static AppWindowManager()
{
resources = new ResourceDictionary[]
{
new ResourceDictionary
{ Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml", UriKind.RelativeOrAbsolute) },
new ResourceDictionary
{ Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml", UriKind.RelativeOrAbsolute) },
new ResourceDictionary
{ Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml", UriKind.RelativeOrAbsolute) },
new ResourceDictionary
{ Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml", UriKind.RelativeOrAbsolute) },
new ResourceDictionary
{ Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml", UriKind.RelativeOrAbsolute) },
new ResourceDictionary
{ Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml", UriKind.RelativeOrAbsolute) }
};
}
protected override Window EnsureWindow(object model, object view, bool isDialog)
{
MetroWindow window = view as MetroWindow;
if (window == null)
{
window = new MetroWindow()
{
Content = view,
SizeToContent = SizeToContent.WidthAndHeight
};
window.MinHeight = 150;
window.MinWidth = 500;
foreach (ResourceDictionary resourceDictionary in resources)
{
window.Resources.MergedDictionaries.Add(resourceDictionary);
}
window.SetValue(View.IsGeneratedProperty, true);
Window owner = this.InferOwnerOf(window);
if (owner != null)
{
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = owner;
}
else
{
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
}
else
{
Window owner2 = this.InferOwnerOf(window);
if (owner2 != null && isDialog)
{
window.Owner = owner2;
}
}
return window;
}
}
This works somewhat but i get a black border around my window until i resize see image below

这有点工作,但我的窗口周围有一个黑色边框,直到我调整大小,见下图

Why is there a black border and how can i get rid of it (If i manually resize the window the border goes away.)?
为什么会有黑色边框,我该如何摆脱它(如果我手动调整窗口大小,边框会消失。)?
采纳答案by Phil
With Caliburn.Micro 2 and Mahapps.Metro 1 the above is no longer a valid combination of the two frameworks.
对于 Caliburn.Micro 2 和 Mahapps.Metro 1,上述不再是两个框架的有效组合。
After some digging through Caliburn.Micro documentations and following out dated guides I came up with the following.
在对 Caliburn.Micro 文档进行了一些挖掘并遵循过时的指南之后,我想出了以下内容。
First create the bootstrapper like so:
首先像这样创建引导程序:
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>();
}
}
Next add the bootstrapper to the App.xaml file along with the MahApps.Metro resources like so:
接下来将引导程序与 MahApps.Metro 资源一起添加到 App.xaml 文件中,如下所示:
<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>
The next step is to ensure your ShellViewModel has the proper exports (this is simple requirements for setting up mef bootstrapper, and don't forget to create the IShell class (not going to show that here as it is standard)) like so:
下一步是确保您的 ShellViewModel 具有正确的导出(这是设置 mef 引导程序的简单要求,并且不要忘记创建 IShell 类(不会在此处显示它是标准的)),如下所示:
[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
}
Last but not least we need to setup the ShellView to use the MahApps.Metro Window:
最后但并非最不重要的是,我们需要设置 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>
Hope this helps anyone else looking to combine Caliburn.Micro and MahApps.Metro in their latest iterations.
希望这可以帮助其他任何希望在最新迭代中结合 Caliburn.Micro 和 MahApps.Metro 的人。
回答by Peter
It seems that the SizeToContent was the problem!
似乎 SizeToContent 是问题所在!
回答by RZa
Solution for that problem is setting ResizeMode property to "NoResize" then border is rendered properly.
该问题的解决方案是将 ResizeMode 属性设置为“NoResize”,然后正确呈现边框。
window = new MetroWindow()
{
Content = view,
SizeToContent = SizeToContent.WidthAndHeight,
ResizeMode = ResizeMode.NoResize
};

