使用Unity bootstrapper时,应用程序资源不可用

时间:2012-07-15 10:52:11

标签: c# wpf prism unity-container

我在应用程序资源

中定义了以下资源
<Application.Resources>
    <Style x:Key="gridTextBox">
        <Setter Property="TextBox.Margin" Value="0,5,5,5"/>
    </Style>
</Application.Resources>

资源照常应用在主窗口的文本框中

<TextBox Name="textBox1" Style="{StaticResource gridTextBox}"/>

我遇到的问题是,如果我通过Unity bootstrapper启动应用程序,应用程序会在文本框资源上抛出XamlParseException。但是如果我在app.xaml上使用startupUri,那么应用程序会按预期加载。我的引导程序看起来像这样

public class Bootstrapper : UnityBootstrapper
{
    protected override void InitializeShell()
    {
        base.InitializeShell();
        App.Current.MainWindow = (Window)Shell;
        App.Current.MainWindow.Show();
    }

    protected override DependencyObject CreateShell()
    {
        var shell = new MainWindow();
        return shell;
    }
}

我的应用程序启动如下

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        new Bootstrapper().Run();
    }
}

我错过了什么?

1 个答案:

答案 0 :(得分:1)

将您的App.xaml更改为此:

<Application x:Class="UnityBootstrapperTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Style x:Key="gridTextBox">
                        <Setter Property="TextBox.Margin" Value="0,5,5,5"/>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>