所以我有一个看似简单的设置,但出于某种原因,我的UWP应用程序没有处理我的Generic.xaml
文件
我有一个以16299为目标的共享类库,其最小版本为16299。
我创建了一个Themes
目录,并在它下面放置了一个Generic.xaml
文件,这是一个资源字典。
在这里面,我尝试添加一个样式,然后按键引用它,但是UWP应用程序找不到样式。
例如
Generic.xaml
<Style TargetType="ListViewItem" x:Key="ListViewItemEvenRowStyle" >
<Setter Property="Padding" Value="0" />
<Setter Property="Background" Value="Black" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
<Style TargetType="ListViewItem" x:Key="ListViewItemOddRowStyle" >
<Setter Property="Padding" Value="0" />
<Setter Property="Background" Value="White" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
当我用StaticResource
引用它时,它会导致此异常。
Windows.UI.Xaml.Markup.XamlParseException:“无法找到与此错误代码相关的文本。 无法找到具有名称/键ListViewItemEvenRowStyle的资源
我还尝试将其放入ResourceDictionary.ThemeResources
中,并将其引用到ThemeResource
中,但我仍然得到了相同的结果。
这是CSProj的定义
<Page Include="Themes\Generic.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
发布于 2019-07-08 02:37:14
您需要使用ResourceDictionary.MergedDictionaries
,而不是ResourceDictionary.ThemeResources。
在您的主要UWP项目中,打开'App.xaml‘文件,添加以下代码:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="YourClassLibrary/Themes/Generic.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
更多信息,请参阅XAML主题资源和ResourceDictionary.MergedDictionaries。
更新于2019/7/10
我很好奇,不碎片整理、Telerik和Microsoft UWP.Toolkit是如何做到这一点的。如果我向项目中添加telerik控件dll并引用其控件,则不需要向资源字典添加任何内容。
泰勒克和WindowsCommunityToolkit是开源的。你可以检查他们的源代码。这些控件是自定义控件。每个控件都有自己的样式文件,通过设置DefaultStyleKey = typeof(YourCustomControlClass)
,它将在控件的构造函数方法中应用于它。
例如,Microsoft.Toolkit.Uwp.UI.Controls/Carousel/控件。您可以看到Carousel.xaml
样式文件与Carousel.cs
类位于同一个文件夹中。在Carousel
构造函数中,它使用DefaultStyleKey = typeof(Carousel);
应用样式。但是仅仅通过这些步骤,系统仍然无法找到相应的样式。您可以看到Microsoft.Toolkit.Uwp.UI.Controls/Themes/文件夹,其中有一个Generic.xaml
文件。它实际上是一本“资源词典”。它使用ResourceDictionary.MergedDictionaries
合并不同自定义控件文件夹中的资源。
这样,主项目只需要添加对控件库的引用,直接使用控件,而不需要在主项目中使用ResourceDictionary.MergedDictionaries
。
https://stackoverflow.com/questions/56906357
复制相似问题