我目前正在嘗試通過在有意義時將其拆分為多個檔案來組織我的 UI Xaml。
因此,我將我的 TitleBar 樣式匯出到一個額外的檔案 ( \UI\Styles\TitleBarButtonStyles.xaml) 中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TitleBarButton" TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="0,0,0,0" />
<Setter Property="Height" Value="18" />
<Setter Property="Width" Value="18" />
<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" CornerRadius="10">
<TextBlock Text="{TemplateBinding Content}" FontFamily="Segoe MDL2 Assets" FontSize="10" HorizontalAlignment="Center" VerticalAlignment="Center" RenderOptions.ClearTypeHint="Auto" TextOptions.TextRenderingMode="Aliased" TextOptions.TextFormattingMode="Display" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--Minimize-->
<Style x:Key="MinimizeButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
<Setter Property="Content" Value="" />
<Setter Property="Background" Value="LimeGreen" />
</Style>
<!--Maximize-->
<Style x:Key="MaximizeButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
<Setter Property="Content" Value="" />
<Setter Property="Background" Value="Orange" />
</Style>
<!--Restore-->
<Style x:Key="RestoreButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
<Setter Property="Content" Value="" />
<Setter Property="Background" Value="Orange" />
<Setter Property="Visibility" Value="Collapsed" />
</Style>
<!--Close-->
<Style x:Key="CloseButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
<Setter Property="Content" Value="" />
<Setter Property="Background" Value="Firebrick" />
</Style>
然后我從主檔案中呼叫它:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/UI/Styles/TitleBarButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
在按鈕上:
<Button Style="{StaticResource MinimizeButton}"
Grid.Column="2"
Click="MinimizeBtn_OnClick" />
<Button x:Name="MaximizeBtn" Grid.Column="3"
Style="{StaticResource MaximizeButton}"
Click="MaximizeBtn_OnClick" />
<Button x:Name="RestoreBtn" Grid.Column="3"
Style="{StaticResource RestoreButton}"
Click="RestoreBtn_OnClick" />
<Button Style="{StaticResource CloseButton}"
Grid.Column="4"
Click="CloseBtn_OnClick" />
但是在所有按鈕上我都會收到錯誤訊息:Ressource 'STYLE_X_KEY_NAME' is not found(STYLE_X_KEY_NAME 是我想要應用的樣式的相應 x:Key)
編輯
兩個檔案都在同一個程式集中。
我已經嘗試了 pack uri 方法:
<ResourceDictionary Source="pack://application:,,,/VRChat Bases Manager;component/UI/Styles/TitleBarButtonStyles.xaml" />
它仍然顯示相同的錯誤。
編輯2
這里的檔案夾結構如下:
主檔案:\UI\Windows\PrimaryWindow.xaml
樣式檔案:\UI\Styles\TitleBarButtonStyles.xaml
編輯 3
出于某種原因,我的檔案\UI\Styles\RoundDropDownStyle.xaml以相同的方式被呼叫。
這里的檔案:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ComboboxTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border CornerRadius="8, 0, 0, 8" BorderThickness="1,1,0,1" Background="{TemplateBinding Background}" BorderBrush="#2A2A2A">
<ScrollViewer x:Name="PART_ContentHost" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ComboboxButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="{TemplateBinding Background}" x:Name="border" CornerRadius="0,8,8,0" BorderThickness="0,1,1,1" BorderBrush="#2A2A2A">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ComboboxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="SelectedIndex" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition MaxWidth="18" />
</Grid.ColumnDefinitions>
<TextBox Background="{TemplateBinding Background}" Name="PART_EditableTextBox" Style="{StaticResource ComboboxTextBoxStyle}" Padding="5,0,0,0" Height="{TemplateBinding Height}" IsReadOnly="True" />
<ToggleButton Background="{TemplateBinding Background}" Grid.Column="1" Margin="0" Height="{TemplateBinding Height}" Style="{StaticResource ComboboxButtonStyle}" Focusable="False" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
<Path Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="DodgerBlue" />
</ToggleButton>
<ContentPresenter Grid.Column="0" Name="ContentSite" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0,0,0" />
<Popup Grid.Column="0" Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border Background="{TemplateBinding Background}" x:Name="DropDownBorder" BorderThickness="1" CornerRadius="5" BorderBrush="#2A2A2A" />
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
uj5u.com熱心網友回復:
好的,我發現了問題:這是檔案上的構建操作屬性。
它被設定為AdditionalFiles。正在作業的檔案將其設定為Page. 一旦我改變它,它就像一個魅力。
顯然這是 Visual Studio 2022 的一種奇怪行為?
當您通過它創建檔案時,Right Click -> Add... -> New Item... -> Resource Dictionary (WPF)它作業正常。
如果你去Right Click -> Add.. -> New From Template... -> Resource Dictionary,它的構建操作設定為AdditionalFiles不起作用。
編輯
我認為這New from Template...實際上是 ReSharper 添加的一個功能。
uj5u.com熱心網友回復:
你的路徑應該是,
<ResourceDictionary x:Name="TitleBarButtonStyles" Source="../Styles/TitleBarButtonStyles.xaml" />
至于您的pack uri 方法,您在 URI 中有空格。考慮處理 Uri 路徑中的空格
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515958.html
標籤:C#wpfxmlwpf 风格
