我正在尝试在 StackLayout
的自定义渲染器中插入特定于UWP的子项 . 但是,在下面的示例代码中, Control
始终是 null
而我的 StackLayout
有子 . 也许 StackPanel
不是 StackLayout
在UWP中呈现的内容 .
public class MyRenderer : ViewRenderer<StackLayout, StackPanel>
{
private bool _childAdded;
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (!_childAdded && Control?.Children != null)
{
_childAdded = true;
Control.Children.Insert(0, new Windows.UI.Xaml.Shapes.Rectangle());
}
base.OnElementPropertyChanged(sender, e);
}
}
3 回答
您的一些修改是cade,因为您在代码实现后调用了 base.OnElementPropertyChanged(sender,e) . 只是尝试使用下面的代码 .
StackLayout
(Layout
)渲染器是ViewRenderer
,并在_W280833上在UWP上实现; Renderer Base Classes and Native Controls .理论渲染器:
来自official document,
UWP平台内的匹配Native控件是LayoutRenderer . 所以它不是直接继承
StackPanel
. 您也可以自定义一个自定义渲染器,如下所示 .对于您的要求,更好的方法是在
Xamarin.Forms
中创建CustomStackLayout
inheritStackLayout
,并在LayoutChildren
覆盖方法中重新布局子元素 . 有关更多详细信息,请参阅Creating a Custom Layout .