Pet*_*ter 14 wpf caliburn.micro mahapps.metro
好吧我试图使用MahApps.Metro和Caliburn.Micro在一起但我遇到了一些问题
这是我的引导程序
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
这有点工作但我在我的窗口周围有一个黑色边框,直到我调整大小看到下面的图像

为什么有黑色边框,我怎么能摆脱它(如果我手动调整窗口大小,边框会消失.)?
小智 10
使用Caliburn.Micro 2和Mahapps.Metro 1,上面不再是两个框架的有效组合.
在通过Caliburn.Micro文档进行一些挖掘并遵循过时的指南后,我想出了以下内容.
首先像这样创建引导程序:
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>();
}
}
Run Code Online (Sandbox Code Playgroud)
接下来将引导程序与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>
Run Code Online (Sandbox Code Playgroud)
下一步是确保你的ShellViewModel具有正确的导出(这是设置mef bootstrapper的简单要求,并且不要忘记创建IShell类(不会在这里显示它是标准的)),如下所示:
[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
}
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是,我们需要设置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>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助其他人在他们最新的迭代中将Caliburn.Micro和MahApps.Metro结合起来.
| 归档时间: |
|
| 查看次数: |
5351 次 |
| 最近记录: |