错误:“;当设置了Source属性时,无法将子项添加到ResourceDictionary&”;

本文关键字:添加 ResourceDictionary 设置 属性 Source 错误 | 更新日期: 2023-09-27 18:25:31

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

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

错误:“;当设置了Source属性时,无法将子项添加到ResourceDictionary&”;

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

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

我甚至不需要重新启动visualstudio。我剪切并粘贴了我的UserControl.Resources,就在我的x:Code上方,错误就消失了。它以前就在它的正下方。