14

好吧,我正在尝试同时使用 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);
    }
}

这是我的 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;
    }
}

这有点工作,但我的窗口周围有一个黑色边框,直到我调整大小,见下图 在此处输入图像描述

为什么有黑色边框,我怎样才能摆脱它(如果我手动调整窗口大小,边框就会消失。)?

4

3 回答 3

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>();
    }
}

接下来将引导程序与 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>

下一步是确保您的 ShellViewModel 具有正确的导出(这是设置 mef 引导程序的简单要求,并且不要忘记创建 IShell 类(这里不显示,因为它是标准的)),如下所示:

[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, 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 的人。

于 2015-02-03T18:43:41.490 回答
6

似乎 SizeToContent 是问题所在!

于 2012-08-21T17:29:14.317 回答
3

该问题的解决方案是将 ResizeMode 属性设置为“NoResize”,然后正确呈现边框。

window = new MetroWindow()
{
    Content = view,
    SizeToContent = SizeToContent.WidthAndHeight,
    ResizeMode = ResizeMode.NoResize
};
于 2012-10-24T13:09:16.343 回答