在.xaml.cs支持代码中创建时,在.xaml中创建错误

时间:2013-06-17 17:35:56

标签: xaml silverlight-5.0 autofac caliburn.micro

我遇到一个奇怪的问题,在我的App.xaml中创建源自Caliburn.Micro.Autofac.AutofacBootstrapper的类失败并出现构建错误:

XML命名空间'clr-namespace:TestProject'中不存在标签'Bootstrapper'

但是,如果我只是在App.xaml.cs中创建引导程序,一切正常。

下面的代码展示了我可以做些什么才能让事情发挥作用。如果我取消注释App.xaml中的Bootstrapper并注释掉App.xaml.cs中的那个,我会收到构建错误。

两者有何不同?

Intellisense对App.xaml中的Bootstrapper很满意我只在构建时遇到错误。

Heres是我的App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:TestProject"
         x:Class="TestProject.App"
         >
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <!--
                <local:Bootstrapper x:Key="Bootstrapper"/>
                -->
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

这是我的App.xaml.cs

namespace TestProject
{
    public partial class App : Application
    {

        public App()
        {
            new Bootstrapper();

            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }
    }
}

这里是Bootsteapper.cs

namespace TestProject
{
    public class Bootstrapper : AutofacBootstrapper<MainPageViewModel>
    {
        public Bootstrapper()
        {
            base.ConfigureBootstrapper();
            EnforceNamespaceConvention = false;
            ViewModelBaseType = typeof(IShell);
        }

        protected override void ConfigureContainer(Autofac.ContainerBuilder builder)
        {
            builder.RegisterType<MainPageViewModel>();

            base.ConfigureContainer(builder);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试将引导程序设置为超出MergedDictionaries?

<Application.Resources>
    <ResourceDictionary>
        <local:Bootstrapper x:Key="Bootstrapper" />
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Assembly;component/OtherResource.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>