我正在构建一个UWP应用程序,并注意到每个页面的xaml往往看起来非常分散。在一个案例中,我使用visualstatemanager有几百行样式代码,然后在页面的实际布局上有一半的代码。
有没有办法将资源拆分成其他文件,以便我的布局和样式相对独立,类似于在网络上使用HTML / CSS的方式?或者我只需要在每个页面上使用这些大块样式?
感谢。
答案 0 :(得分:0)
当然有。只需创建一个资源字典并在其中应用一些样式(不要忘记将x:Key放到你的样式中,你需要它来“调用”其他页面的样式),如下所示:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="GridViewItem" x:Key="CustomGridviewStyle">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,0,4,4"/>
<Setter Property="MinWidth" Value="{ThemeResource GridViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource GridViewItemMinHeight}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewItem">
<ListViewItemPresenter
ContentTransitions="{TemplateBinding ContentTransitions}"
SelectionCheckMarkVisualEnabled="True"
CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
CheckBoxBrush="{ThemeResource SystemControlBackgroundChromeMediumBrush}"
DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
PointerOverForeground="{ThemeResource SystemControlForegroundBaseHighBrush}"
SelectedBackground="{ThemeResource SystemControlHighlightAccentBrush}"
SelectedForeground="{ThemeResource SystemControlForegroundBaseHighBrush}"
SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
ReorderHintOffset="{ThemeResource GridViewItemReorderHintThemeOffset}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
ContentMargin="{TemplateBinding Padding}"
CheckMode="Overlay"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
之后,您可以将此词典放入应用程序xaml,以便不将其定义到您使用的每个页面。像那样:
<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="test.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
最后你可以这样称呼这样的风格:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView Name="gvTest"
ItemContainerStyle="{StaticResource CustomGridviewStyle}">
</GridView>
</Grid>
</Page>
度过愉快的一天。
答案 1 :(得分:0)
在WPF中,语法使用URI。它有两种样式,从"pack://"
或appointing as a relative path
开始。
例如:"/ResourceWPF;component/xxxFolder/xxx.xaml"
而UWP使用模式:"ms-appx:///ResourceUWP/xxxFolder/xxx.xaml"
顺便说一句,视觉工作室有时候会犯一些难以置信的错误。例如Page.Resources
只能设置一次。将<ResourceDictionary>
标记带到文章末尾。
在app.xaml.cs中设置ResourceDictionary会更好。这应该在环境中起作用。如果这不起作用,请尝试将ClassLibrary添加到解决方案中,将ResourceDictionary添加到ClassLibrary。然后使用
"ms-appx:///App_name/file_name.xaml"
。希望这有帮助。