【发布时间】:2020-05-14 20:30:27
【问题描述】:
我想在作为主题的 ResourceDictionary 中绑定一个值。
我在https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/theming 中学习了一些教程
这是名为 Black.xaml 的 ResourceDictionary 的代码:
<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Themes.Black">
<Color x:Key="PrimaryBackground" x:FactoryMethod="FromHex">
<x:Arguments>
<x:String>#101010</x:String>
</x:Arguments>
</Color>
</ResourceDictionary>
这是 App.xaml 中的代码:
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Test.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Black.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
这里是 ContentView:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test.Views"
xmlns:VM="clr-namespace:Test.ViewModels"
mc:Ignorable="d"
x:Class="Test.Views.MainView">
<ContentView.BindingContext>
<VM:MainViewVM></VM:MainViewVM>
</ContentView.BindingContext>
<ContentView.Content>
<Grid BackgroundColor="">
</Grid>
</ContentView.Content>
</ContentView>
现在我想将上面 Grid 的 BackgroundColor 属性绑定到 ResourceDictionary 的值 PrimaryBackground。
我怎样才能实现它?谢谢。
【问题讨论】:
标签: xamarin xamarin.forms