问题描述
我想为我的应用制作一个通用设置页面。我有一个不同类型的设置列表,每一个对应如下界面:
public interface ISettings
{
string SectionLabel { get; }
View SectionView { get; }
}
每种类型的设置代表应用程序设置的一个给定方面或部分。目标是让每种类型的设置定义自己的外观和调整自己值的方式。
我有一个包含所有设置列表的设置页面: public List settingsSections { get => App.AppSettings.SettingsSections; }
<StackLayout BindableLayout.ItemsSource="{Binding settingsSections,Source={x:Reference this}}" Padding="0" BackgroundColor="White">
<BindableLayout.ItemTemplate>
<DataTemplate>
<xct:Expander>
<xct:Expander.Header>
<StackLayout Orientation="Horizontal">
<Image Source="{Helpers:ImageResource Images.filledDownArrow.png}" HeightRequest="{Binding Source={x:Reference heightRef},Path=Height,Converter={Helpers:IntMultiplier Multiplier=0.99}}"
HorizontalOptions="Start"
VerticalOptions="Center">
<Image.Triggers>
<DataTrigger targettype="Image"
Binding="{Binding Source={RelativeSource AncestorType={x:Type xct:Expander}},Path=IsExpanded}"
Value="True">
<Setter Property="Source"
Value="{Helpers:ImageResource Images.filledRightArrow.png}" />
</DataTrigger>
</Image.Triggers>
</Image>
<Label x:Name="heightRef" Text="{Binding SectionLabel}"
FontAttributes="Bold"
FontSize="Medium" />
</StackLayout>
</xct:Expander.Header>
<xct:Expander.Content="{Binding SectionView}"/>
</xct:Expander>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
当然,这条线是行不通的:
<xct:Expander.Content="{Binding View}"/>
我怎样才能实现我的目标?有人能帮我吗 ?我想我不能使用自定义控件,因为我应该根据所引用的设置的实际类型为 stacklayout 的每一行/部分更改控件的类型。
解决方法
选项 1
此时您不必指定 xct:Expander.Content
,无论您放在哪里,都会隐式设置 Content 属性。
替换
<xct:Expander.Content="{Binding SectionView}"/>
与
<ContentView Content="{Binding SectionView}"/>
选项 2
以这种方式指定:
<DataTemplate>
<xct:Expander Content="{Binding SectionView}">
<xct:Expander.Header>
...