【问题标题】:.NET 6 - WPF CustomControl Template not applied though being in App Resources.NET 6 - WPF 自定义控件模板未通过在应用程序资源中应用
【发布时间】:2022-08-17 23:19:14
【问题描述】:

我们正在切换一个巨大的 WPF Appl。到 .NET 6.0。至少有一个适用于 4.8 的 CustomControl 没有应用通过 Generic.xaml 引用的模板。我不确定Generic.xaml 是否未加载或需要考虑其他问题。 可以通过简单的“添加”调用手动将样式添加到Application.Resources.MergedDictionaries,并将源设置为它。之后我可以看到自定义控件样式,其中键是正确的类型。它仍未应用,因为没有可视化表示,也没有调用覆盖的 OnApplyTemplate 方法。 如果一切都失败了,如果我有给定的样式,我可以手动应用模板吗,就像手动将样式应用到新创建的实例一样?

另外:项目现在是 SDK 样式,AssemblyInfo.cs 已被接管,“生成程序集信息”设置为 false。它包含标准的ThemeInfo 条目。

只是为了澄清一下成功找到Generic.xaml的代码。但是在 6.0 Generic.xaml 之前加载而没有做任何事情。

    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
    {
          Source = new Uri(\"/Contracts;component/Themes/Generic.xaml\", UriKind.Relative)
    });

谢谢大家的帮助!

    标签: c# wpf custom-controls .net-6.0 visual-studio-2022


    【解决方案1】:

    我有同样的问题。

    也许您需要在您的 CustomControlLibrary 的 TopLevel 添加 Assembly.cs

    using System.Windows;
        
    [assembly: ThemeInfo(
        ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                         //(used if a resource is not found in the page,
                                         // or application resource dictionaries)
        ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
    )]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多