简体   繁体   English

如何在 WPF 和 XAML 群岛中使用 Windows 10 样式资源

[英]How to use Windows 10 style resource in WPF with XAML Islands

I'm using XAML Islands to make my app and I want to use Windows 10 styling in my WPF app like here .我正在使用 XAML Islands 来制作我的应用程序,我想在我的 WPF 应用程序中使用 Windows 10 样式,就像这里一样。 For example <TextBlock Text="Header" Style="{StaticResource HeaderTextBlockStyle}"/> would result in:例如<TextBlock Text="Header" Style="{StaticResource HeaderTextBlockStyle}"/>将导致:

在此处输入图像描述

But this doesn't work in WPF (It does work in UWP without any modifications), my understanding is that XAML Islands should make it possible.但这在 WPF 中不起作用(它在 UWP 中起作用,无需任何修改),我的理解是 XAML Islands 应该使它成为可能。 When I try to simply add the code above to my xaml file I get the exception当我尝试简单地将上面的代码添加到我的 xaml 文件中时,我得到了异常

Cannot find resource named 'HeaderTextBlockStyle'.找不到名为“HeaderTextBlockStyle”的资源。 Resource names are case sensitive.资源名称区分大小写。

I get the same exception if I add Style="{StaticResource HeaderTextBlockStyle}" to a <xamlhost:WindowsXamlHost> element.如果我将Style="{StaticResource HeaderTextBlockStyle}"添加到<xamlhost:WindowsXamlHost>元素,我会得到相同的异常。

So I tried to add the controls with code so I added this WindowsXamlHost control as a stackpanel:因此,我尝试使用代码添加控件,因此我将此 WindowsXamlHost 控件添加为堆栈面板:

<xamlhost:WindowsXamlHost InitialTypeName="Windows.UI.Xaml.Controls.StackPanel" ChildChanged="WindowsXamlHost_ChildChanged"/>

And added this method (an event handler that is ran when the control is made. Learned it from this ) that handles adding additional controls (a TextBlock) to the StackPanel:并添加了这个方法(一个在制作控件时运行的事件处理程序。从中学习), 处理向 StackPanel 添加额外的控件(一个 TextBlock):

private void WindowsXamlHost_ChildChanged(object sender, EventArgs e)
    { 
        // Get the host control
        WindowsXamlHost host = (WindowsXamlHost)sender;
        // Get the StackPanel in the host
        Windows.UI.Xaml.Controls.StackPanel sp = (Windows.UI.Xaml.Controls.StackPanel)host.Child;
        // Make a TextBlock to add to the StackPanel
        Windows.UI.Xaml.Controls.TextBlock textBlock = new Windows.UI.Xaml.Controls.TextBlock();
        // Set the text of the TextBlock
        textBlock.Text = "LockCursorInMonitor";
        // Get the style resources, cast them to the appropriate type for XAML Islands and add them to the TextBlock
        textBlock.Style = (Windows.UI.Xaml.Style)Application.Current.Resources["HeaderTextBlockStyle"];

        // Another way to get resources but this doesn't work too.
        //textBlock.Style = (Windows.UI.Xaml.Style)this.FindResource("HeaderTextBlockStyle");

        // Add the TextBlock to the stackpanel
        sp.Children.Add(textBlock);
    }

The Application.Current.Resources["HeaderTextBlockStyle"] way does nothing and doesn't throw an exception. Application.Current.Resources["HeaderTextBlockStyle"]方式什么都不做,也不会抛出异常。

The this.FindResource("HeaderTextBlockStyle") way throws the next exception: this.FindResource("HeaderTextBlockStyle")方式抛出下一个异常:

System.Windows.ResourceReferenceKeyNotFoundException: ''HeaderTextBlockStyle' resource not found.' System.Windows.ResourceReferenceKeyNotFoundException: ''HeaderTextBlockStyle' 资源未找到。'

So how can I get these style resources in my WPF app?那么如何在我的 WPF 应用程序中获取这些样式资源?

One way to achieve this is to use the package ModernWPF but then you lose all the benefits of XAML Islands (if there are any. Everything I needed from XAML Islands is in ModernWPF and is easier to implement).实现此目的的一种方法是使用 package ModernWPF ,但随后您将失去 XAML 群岛的所有好处(如果有的话。我需要从 XAML 中获得的一切。并且在现代 WPF 中更容易实现岛屿)

After installing and setting ModernWPF up you can simply use the <TextBlock Text="Header" Style="{StaticResource HeaderTextBlockStyle}"/> way and it just works.安装和设置 ModernWPF 后,您可以简单地使用<TextBlock Text="Header" Style="{StaticResource HeaderTextBlockStyle}"/>方式,它就可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 XAML 岛的 WPF 中使用正确的 WindowsRuntime.dll? - How to use the correct WindowsRuntime.dll in WPF for XAML Islands? Windows 10 的真正最低支持版本是什么,用于在使用 Z8CBFD56512569C43ZDEDB 的 WPF 应用程序中托管 UWP 控件? - What is the true minimum supported version of Windows 10 for hosting UWP controls in a WPF application using XAML Islands? wpf如何在xaml图像中使用资源中的jpg? - wpf how to use jpg from resource in xaml-image? 如何在WPF XAML中的DataGridCell样式的多触发条件中使用行索引作为条件? - How to use row index as a condition in a multitrigger for a DataGridCell style in WPF XAML? 如何在WPF应用中使用Windows 10功能 - How to use Windows 10 features in WPF app 如何在Outlook插件中使用Windows 8默认样式(Xaml) - How to use the Windows 8 default style (Xaml) in an Outlook plugin 如何在XAML Windows 10中为所有网格使用自适应UI? - How to use Adaptive UI for all Grid in XAML Windows 10? WPF上下文菜单不是Windows 10样式 - WPF Context Menu is not in Windows 10 Style Windows 10 WPF Xaml ListView项目样式与Windows 7不匹配 - Windows 10 WPF Xaml ListView Item Styles Not Matching Windows 7 如何向datagridtemplatecolumn(WPF XAML)添加样式 - How to add style to datagridtemplatecolumn (WPF XAML)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM