[英]Xamarin forms contentpresenter goes in wrong row and empty on phone
我正在嘗試使用Xamarin Forms創建我的自定義“GroupBox”控件,它只是一個有兩行的網格。 第一行應該只顯示一個帶標題的框。 標題有一個帶標簽的背景矩形。 第二行通過ContentPresenter
顯示內容,第二行中的內容如下。
我相信我已經正確地完成了XAML,這就是我在WPF
,它在GroupBox.xaml
Forms Previewer中顯示得很好,但當我將它添加到主頁面時,Groupbox的內容進入了第一行(標題)而不是第二行由於某種原因在預覽器中。
此外,當我嘗試在Android手機上運行時,它看起來像這樣:
組框是空白框架,沒有背景顏色或文本
這是GroupBox'用戶控件'的代碼
<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XForms.GroupBox">
<Grid.RowDefinitions>
<RowDefinition Height="0.2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Frame Grid.Row="0" Grid.RowSpan="2" Margin="1" OutlineColor="AliceBlue"/>
<BoxView Grid.Row="0" BackgroundColor="Red" Margin="1"/>
<Label Grid.Row="0" TextColor="White" Text="Label!" Margin="2" />
<Grid Grid.Row="1">
<ContentPresenter/>
</Grid>
</Grid>
此代碼的主要形式為:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XForms;assembly=XForms"
x:Class="XForms.MainPage">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<local:GroupBox Grid.Row="0">
<Label Text="I'm some content 1"/>
</local:GroupBox>
<local:GroupBox Grid.Row="1">
<Label Text="I'm some content 2"/>
</local:GroupBox>
<local:GroupBox Grid.Row="2">
<Label Text="I'm some content 3"/>
</local:GroupBox>
</Grid>
</Grid>
</ContentPage>
在主頁XAML中我可以向標簽添加網格行,即使內容中沒有網格,它也可以在預覽器中使用 - 但它在手機上仍然是空白的(這是最重要的)。
<local:GroupBox Grid.Row="0">
<Label Text="I'm some content 1" Grid.Row="1"/>
</local:GroupBox>
我不知道Frame
是什么,但它目前覆蓋了BoxView
和標簽,因此使它們不可見。 注釋掉Frame
,您將再次看到標簽和BoxView
。
我相信我已經正確地完成了XAML,這就是我在WPF中的表現,它在GroupBox.xaml Forms Previewer中顯示得很好,但當我將它添加到主頁面時,Groupbox的內容進入了第一行(標題)而不是第二行由於某種原因在預覽器中。
ContentPresenter
不能像那樣工作。在Xamarin.Forms中,它通常用於ControlTemplate
。 有關ContentPresenter的使用,請參閱此博客 。
在您的情況下,如果您希望ContentPresenter工作。 您可以將ContentView
用於GroupBox
,而不是使用Grid
for GroupBox
,如下所示:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GroupBox : ContentView
{
public GroupBox()
{
InitializeComponent();
}
}
GroupBox.xaml:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Demo.GroupBox">
<ContentView.ControlTemplate>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<!--<Frame Grid.Row="0" Grid.RowSpan="2" Margin="1" OutlineColor="AliceBlue"></Frame>-->
<BoxView Grid.Row="0" BackgroundColor="Red" Margin="1" />
<Label Grid.Row="0" TextColor="White" Text="Label!" Margin="2" />
<Grid Grid.Row="1">
<ContentPresenter/>
</Grid>
</Grid>
</ControlTemplate>
</ContentView.ControlTemplate>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.