【问题标题】:Error: "Cannot add children to a ResourceDictionary when the Source property is set."错误:“设置 Source 属性后,无法将子级添加到 ResourceDictionary。”
【发布时间】:2014-11-28 15:37:38
【问题描述】:

我尝试添加一个全局样式,该样式将应用于我的应用中的所有控件。

我在 app.xaml 中添加了我的样式:

<Application x:Class="SimulatorUi.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <Style TargetType="Button">
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="FontSize" Value="24"></Setter>
        </Style>

    </Application.Resources>
</Application>

它在测试应用中运行良好。但是当我将样式添加到我的真实应用程序时,我收到编译器错误: “设置 Source 属性时,无法将子级添加到 ResourceDictionary。”。

我无法弄清楚我做错了什么。任何帮助将不胜感激?

注1: 我可以毫无问题地添加资源字典:

<Application x:Class="WpfAppTestResource.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

注2:(仅与原始问题相关) 我的应用程序使用“Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase”来确保只运行一个实例。我在 _application.Run() 之前错过了“InitializeComponent()”,这是必不可少的。我解决了一个问题,但我仍然遇到最初的问题,而且我使用 SingleInstance 模式进行的测试工作正常。只有我的真实应用无法正常运行。

用于单实例的代码:

namespace WpfApplication1.SingleInstanceApp
{
    public class SingleInstanceManager : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        private App _application;
        private System.Collections.ObjectModel.ReadOnlyCollection<string> _commandLine;

        private static readonly SingleInstanceManager _instance = new SingleInstanceManager();

        private SingleInstanceManager()
        {
            IsSingleInstance = true;
        }

        public static SingleInstanceManager Instance
        {
            get { return _instance; }
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            // First time _application is launched
            _commandLine = eventArgs.CommandLine;

            _application = new App();

            // Missing line in original code which I had to adapt.
            // _application.InitializeComponent();
            if (_contentLoaded)
            {
                return false;
            }
            _contentLoaded = true;

            // this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
            System.Uri resourceLocater = new System.Uri("/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/app.xaml", System.UriKind.Relative);
            System.Windows.Application.LoadComponent(_application, resourceLocater);

            _application.Run();
            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            // Subsequent launches
            base.OnStartupNextInstance(eventArgs);
            _commandLine = eventArgs.CommandLine;
            _application.Activate();
        }
    }
}


namespace WpfApplication1.SingleInstanceApp
{
    public class EntryPoint
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceManager manager = SingleInstanceManager.Instance;
            manager.Run(args);
        }
    }
}


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        // ******************************************************************
        protected override void OnStartup(StartupEventArgs e)
        {
            // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            // TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

            base.OnStartup(e);
            this.MainWindow = new MainWindow();
            MainWindow.Show();
        }

        // ******************************************************************
        public void Activate()
        {
            MainWindow.Activate();
        }

        // ******************************************************************
    }
}

【问题讨论】:

    标签: c# xaml resources styles global


    【解决方案1】:

    我通过添加等效于 _application.InitializeComponent(); 的代码解决了部分问题在 SingleInstanceManager 的 OnStartup 中。 在我修复了 OnStartup 中的 bug 后,问题仍然存在。

    我重新启动了 Visual Studio,问题就消失了!!!呸呸呸呸!!!

    【讨论】:

    • 是的,我可以确认重新启动 Visual Studio 可以解决问题。
    • 确认这仍然发生在 VS2015 中。
    • VS2015,重新启动 Visual Studio 解决了我的问题。谢谢!
    【解决方案2】:

    我什至不必重新启动 Visual Studio。我在 x:Code 上方剪切并粘贴了 UserControl.Resources,错误就消失了。之前就在它的正下方。

    【讨论】:

    • 谢谢。我会让我的回答保持原样,直到你得到 2 票。如果你有它们。我会接受您的回答,因为它更容易(请告诉我)。但我更愿意等待,看看其他人是否可以像你一样解决他们的问题。我无法重现此问题以验证它是否总能解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2019-12-10
    • 2011-04-03
    • 1970-01-01
    相关资源
    最近更新 更多