您可以使用 XAML 为您的应用定义 UI 或资源。
资源通常是您希望多次使用的某些对象的定义。若要稍后引用 XAML 资源,请为资源指定一个与其名称类似的键。
您可以在整个应用程序中或从其中的任何 XAML 页面引用资源。
您可以使用 Windows 运行时 XAML 中的 ResourceDictionary 元素定义您的资源。
然后,您可以使用 StaticResource 标记扩展或 ThemeResource 标记扩展来引用您的资源。
资源不必是字符串。
它们可以是任何可共享的对象,例如样式、模板、画笔和颜色。但是,控件、形状和其他 FrameworkElements 是不可共享的,因此不能将它们声明为可重用资源。
例子:
<Page.Resources>
<x:String x:Key="key1">Hey</x:String>
<x:String x:Key="key2">Nice</x:String>
</Page.Resources>
您可以通过在适当位置寻址键来使用这些资源,即:
<Label Text="{StaticResource key1}" FontSize="Large" VerticalAlignment="Center"/>
好吧,为了使您的项目更有条理,您需要将 ResourceDictionary 设为一个单独的文件并像这样调用它(ContentPage 部分取决于页面):
<ContentPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ContentPage.Resources>
// 在此示例中,样式位于同一文件夹中,您可以创建动态资源以从所有区域访问它或以适当的方式创建路径,例如:
xmlns:themes = "clr-namespace:AppName.Themes;assembly=AppName"
我们如何提供一个 ResourceDictionary Source URI 让设计者满意并且不需要指定程序集名称?
你做了一个动态的。
像这样:
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="App.Themes.Theme"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Color x:Key="PrimaryColor">#ffffff</Color>
<Color x:Key="PrimaryDarkColor">#0f0f0f</Color>
</ResourceDictionary>
并在 app.xaml 中执行此操作(如果主题位于主项目的主题文件夹中):
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:themes = "clr-namespace:YourProjectName.Themes;assembly=YourProjectName"
x:Class="YourProjectName.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<themes:Theme />
</ResourceDictionary.MergedDictionaries >
</ResourceDictionary>
</Application.Resources>
那么你可以在任何地方做这样的事情:
BackgroundColor="{DynamicResource PrimaryColor}"
祝你好运!