我使用 Xamarin Forms 在 XAML 中有這個(簡化的)布局:
<controls:MyTabItemView>
<ScrollView>
<Grid>
......
<controls:CustomButton x:Name="XYZ"/>
......
</Grid>
</ScrollView>
</controls:TabItemView>
這在 iOS 中很好,但在 Android 中,我不想要 ScrollView,主容器應該是 Grid,比如:
<controls:MyTabItemView>
<Grid>
......
<controls:CustomButton x:Name="XYZ"/>
......
</Grid>
</controls:TabItemView>
如果我嘗試使用<OnPlatform>and<On Platform="Android" / "iOS">范例創建 2 個布局(基本上在 XAML 中復制所有,但<ScrollView>在 Android 部分中洗掉),這幾乎可以作業,但由于XYZ它會導致構建錯誤,因為.g.cs檔案將宣告XYZ兩次。這是一個已知錯誤,請參閱:Xamarin OnPlatform - Duplicate Names
所以我在 XAML 中做不到,我嘗試在視圖建構式中破解 xaml.cs,
if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android)
{
Children[0] = Children[0].Children[0]; //get rid of ScrollView
}
這不會按原樣Children編譯IReadOnlyList。
任何建議,這里最好的解決方案是什么,不要在 Android 的情況下添加 ScrollView,而是從 Grid 作為主容器開始?
謝謝
uj5u.com熱心網友回復:
由于您知道創建的布局,因此InitializeComponent();您可以通過以下方式進行替換:
var topLayout = (ScrollView)Children[0];
Children.RemoveAt(0);
Children.Insert(0, topLayout.Children[0]);
這用ScrollView它的第一個孩子替換了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478218.html
標籤:xml xamarin.forms 布局 平台
上一篇:由于資料系結(基本資料系結),我的Xamarin應用程式不斷崩潰
下一篇:系結不更新WinUI3
