UWP/WPF- 有没有比按页管理资源更好的方法?
UWP/WPF- Is there a better way to manage resources other than a per-page basis?
我正在构建一个 UWP 应用程序,我注意到每个页面的 xaml 看起来都非常分散。有一种情况,我有几百行使用 visualstatemanager 的样式代码,然后一半的代码用于页面的实际布局。
有没有办法将资源拆分到其他文件中,以便我的布局和样式彼此相对独立,类似于您在网络上的 HTML/CSS?或者我是否只需要忍受每个页面上的这些大块样式?
谢谢。
当然有。只需创建一个资源字典并在其中应用一些样式(不要忘记将 x:Key 放入您的样式中,您将需要它 "call" 其他页面的样式),如下所示:
<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>
祝你有愉快的一天。
在WPF中,语法使用URI。它有两种样式,以 "pack://"
或 appointing as a relative path
.
开头
例如:"/ResourceWPF;component/xxxFolder/xxx.xaml"
- 第一个表示应用程序的名称。
- 第二种表示字典是应用程序文件夹下的一个组件。
而 UWP 使用模式:"ms-appx:///ResourceUWP/xxxFolder/xxx.xaml"
顺便说一句,visual studio有时会犯一些令人难以置信的错误。比如Page.Resources
就只能设置一次。带<ResourceDictionary>
标签到文章末尾。
- 清洁溶液
- 构建解决方案
- 如果无法通过,请重新打开您的 visual studio。
It's better to set the ResourceDictionary in app.xaml.cs. That should be worked in the environment. If this cannot work, try to add a ClassLibrary to the solution, add the ResourceDictionary to ClassLibrary. Then you use "ms-appx:///App_name/file_name.xaml"
. Hope this help.
我正在构建一个 UWP 应用程序,我注意到每个页面的 xaml 看起来都非常分散。有一种情况,我有几百行使用 visualstatemanager 的样式代码,然后一半的代码用于页面的实际布局。
有没有办法将资源拆分到其他文件中,以便我的布局和样式彼此相对独立,类似于您在网络上的 HTML/CSS?或者我是否只需要忍受每个页面上的这些大块样式?
谢谢。
当然有。只需创建一个资源字典并在其中应用一些样式(不要忘记将 x:Key 放入您的样式中,您将需要它 "call" 其他页面的样式),如下所示:
<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>
祝你有愉快的一天。
在WPF中,语法使用URI。它有两种样式,以 "pack://"
或 appointing as a relative path
.
例如:"/ResourceWPF;component/xxxFolder/xxx.xaml"
- 第一个表示应用程序的名称。
- 第二种表示字典是应用程序文件夹下的一个组件。
而 UWP 使用模式:"ms-appx:///ResourceUWP/xxxFolder/xxx.xaml"
顺便说一句,visual studio有时会犯一些令人难以置信的错误。比如Page.Resources
就只能设置一次。带<ResourceDictionary>
标签到文章末尾。
- 清洁溶液
- 构建解决方案
- 如果无法通过,请重新打开您的 visual studio。
It's better to set the ResourceDictionary in app.xaml.cs. That should be worked in the environment. If this cannot work, try to add a ClassLibrary to the solution, add the ResourceDictionary to ClassLibrary. Then you use
"ms-appx:///App_name/file_name.xaml"
. Hope this help.