gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:13 24 4
gpt4 key购买 nike

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

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com