我在查找 style.xaml 资源时遇到问题,该资源包含按钮、文本框和其他元素的样式。此资源采用字典 XAML 的形式,位于主题文件夹中,虽然该文件夹中的用户控件可以识别它,但当我在 HomeWindow 中使用此控件时,会发生错误,指示 style.xaml 资源找不到。
您能否告知需要采取哪些步骤来解决此问题?
HomeWindow.xaml:
<Window x:Class="Wallet.Views.HomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wallet.Views"
xmlns:UserControls="clr-namespace:Wallet.Themes.UserControls"
mc:Ignorable="d"
Title="Home" Height="650" Width="900" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid Background="#FF1A1919">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<UserControls:SidebarMenu/>
<Grid Grid.Column="1" Background="#FF1A1919">
</Grid>
</Grid>
</Window>
SidebarMenuWindow.xaml:
<UserControl x:Class="Wallet.Themes.UserControls.SidebarMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Wallet.Themes.UserControls"
xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="170">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid Background="#272829">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition MinHeight="30"/>
<RowDefinition MinHeight="30"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Height="90" Margin="2 30 2 2" HorizontalAlignment="Center" VerticalAlignment="Center">
<svgc:SvgViewbox Source="/Assets/Vectors/person-account.svg" />
</Label>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
</Grid>
<StackPanel Grid.Row="1">
<Button Content="HOME" Style="{StaticResource lineBttn}" IsDefault="True"/>
<Button Content="PLANOWANIE WYDATKÓW" Style="{StaticResource lineBttn}" />
<Button Content="RAPORTY I STATYSTKI" Style="{StaticResource lineBttn}" />
<Button Content="PROGNOZA" Style="{StaticResource lineBttn}" />
<Button Content="KONTA" Style="{StaticResource lineBttn}" />
<Button Content="KATEGORIE" Style="{StaticResource lineBttn}" />
<Button Content="USTAWIENIA" Style="{StaticResource lineBttn}" />
<Button Content="WYLOGUJ SIĘ" Style="{StaticResource lineBttn}" />
</StackPanel>
</Grid>
</UserControl>
样式.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="lineBttn" TargetType="Button">
<Setter Property="MinHeight" Value="32"/>
<Setter Property="Width" Value="170"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="#FFF6E0"/>
</Trigger>
<Trigger Property="IsDefault" Value="True">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="#FFF6E0"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="whiteBttn" TargetType="Button">
<Setter Property="MinHeight" Value="35"/>
<Setter Property="Width" Value="270"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="textbox1" TargetType="TextBox">
<Setter Property="MinHeight" Value="35"/>
<Setter Property="Width" Value="270"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="CaretBrush" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="white"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="white"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="password" TargetType="PasswordBox">
<Setter Property="MinHeight" Value="35"/>
<Setter Property="Width" Value="270"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="CaretBrush" Value="White"/>
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="white"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="white"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
更新
ResourceDictionary
中
HomeWindow
的来源
<ResourceDictionary Source="../Themes/Style.xaml"/>
也就是说,如果 Style.xaml 文件确实位于主题文件夹中,则
"../Themes/UserControls/Style.xaml"
如果它位于 UserControls 文件夹中。
您还可以通过在 App.xaml 中添加资源字典来全局使用它们
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--<ResourceDictionary Source="Themes/UserControls/Style.xaml"/>-->
<ResourceDictionary Source="Themes/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>