c# - 错误 : "Cannot add children to a ResourceDictionary when the Source property is set."

标签 c# xaml resources styles global

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

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

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

最佳答案

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

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

关于c# - 错误 : "Cannot add children to a ResourceDictionary when the Source property is set.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26181304/

相关文章:

c# - 反序列化异常处理

c# - 在 C# 中序列化对象时如何指定 XML 编码

resources - AngularJS 指令和服务($resource)之间的冲突

java - 调度程序 servlet 和 mvc :resources conflict error: No matching constant for [0]

xaml - 单独文件夹中的 ListView DataTemplate

php - PHP 可以返回 ID# 0 的资源吗?

c# - C# 中的 Maybe/Option monad

c# - 如何清除浏览器后退按钮点击 MVC4 中的浏览器缓存?

wpf - 为什么 UI 的可伸缩性在 WPF 中很重要?

visual-studio-2010 - 如何修复值不能为空参数名称 : typeName when opening XAML file?