這里contentItems[0]有內容標題和contentItems[1]標題段落。我想在 Xamarin Expander 的標題內顯示段落。
運行此代碼后得到System.NullReferenceException: 'Object reference not set to an instance of an object。
public partial class TermsAndConditionsPage : ContentPage
{
private TermsAndConditionsViewModel _Model;
public TermsAndConditionsPage()
{
InitializeComponent();
_Model = new TermsAndConditionsViewModel(Navigation);
BindingContext = _Model;
for (int i = 1; i < _Model.contentList.Length; i )
{
string[] contentItems = _Model.contentList[i].Split("\n", 2);
Console.WriteLine("Printing Header of Content... \n");
Console.WriteLine(contentItems[0]);
Console.WriteLine("Printing Paragraph of Header... \n");
Console.WriteLine(contentItems[1]);
Expander expander = new Expander
{
Header = new Label
{
Text = contentItems[0],
FontAttributes = FontAttributes.Bold,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
}
};
Grid grid = new Grid
{
Padding = new Thickness(10),
ColumnDefinitions =
{
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = GridLength.Auto }
}
};
grid.Children.Add(new Label
{
Text = contentItems[1],
FontAttributes = FontAttributes.Italic
}, 1, 0);
expander.Content = grid;
}
}
}
像這張圖片一樣的輸出,但我需要多個擴展器取決于陣列。

<ScrollView Margin="20">
<StackLayout BindableLayout.ItemsSource="{Binding roots}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<xct:Expander>
<xct:Expander.Header>
<Label Text="{Binding Root}"
FontAttributes="Bold"
FontSize="Large" />
</xct:Expander.Header>
<StackLayout BindableLayout.ItemsSource="{Binding Node}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<xct:Expander Padding="10">
<xct:Expander.Header>
<Label Text="{Binding Key.Node}" FontSize="Medium" />
</xct:Expander.Header>
<StackLayout BindableLayout.ItemsSource="{Binding Value}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding SubRoot}" FontSize="Small" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</xct:Expander>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</xct:Expander>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ScrollView>
有關視圖模型的更多詳細資訊,您可以參考我之前完成的執行緒。 xamarin 表單中的多級串列視圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480843.html
