首页 文章

Xamarin Forms - UWP自定义渲染器:如何将子项添加到stacklayout

提问于
浏览
1

我正在尝试在 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 回答

  • 0

    您的一些修改是cade,因为您在代码实现后调用了 base.OnElementPropertyChanged(sender,e) . 只是尝试使用下面的代码 .

    public class MyRenderer : ViewRenderer<StackLayout, StackPanel>
    {
        private bool _childAdded;
    
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
             base.OnElementPropertyChanged(sender, e);
             if(Control==null)
              return;
            if (!_childAdded && Control.Children != null)
            {
                _childAdded = true;
                Control.Children.Insert(0, new Windows.UI.Xaml.Shapes.Rectangle());
            }
    
        }
    }
    
  • 0

    StackLayoutLayout )渲染器是 ViewRenderer ,并在_W280833上在UWP上实现; Renderer Base Classes and Native Controls .

    理论渲染器:

    public class MyRenderer : ViewRenderer<StackLayout, FrameworkElement>
    ...
    
  • 0

    控件始终为null,而我的StackLayout则为Children . 也许StackPanel .

    来自official document

    在Xamarin.Forms中,所有布局类都派生自Layout <T>类,并将泛型类型约束为View及其派生类型 . 但是儿童元素的布局是不正确的 .

    UWP平台内的匹配Native控件是LayoutRenderer . 所以它不是直接继承 StackPanel . 您也可以自定义一个自定义渲染器,如下所示 .

    [assembly: ExportRenderer(typeof(StackLayout), typeof(ICustomStackLayoutRenderer))]
    
    namespace CustomStackLayoutRenderer.UWP
    {
        public class ICustomStackLayoutRenderer : ViewRenderer<StackLayout, StackPanel>
        {
            private bool _childAdded;
            protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
            {
                base.OnElementChanged(e);
                if (Control == null)
                {
                    var stacklayout = new StackPanel();
                    SetNativeControl(stacklayout);
                }
                if (e.OldElement != null)
                {
    
                }
                if (e.NewElement != null)
                {
                    if (!_childAdded && Control.Children != null)
                    {
                        _childAdded = true;
                        Control.Children.Insert(0, new Windows.UI.Xaml.Shapes.Rectangle() { Width = 100, Height = 100, Fill = new SolidColorBrush(Colors.Red) });
                    }
                }
    
            }
        }
    }
    

    对于您的要求,更好的方法是在 Xamarin.Forms 中创建 CustomStackLayout inherit StackLayout ,并在 LayoutChildren 覆盖方法中重新布局子元素 . 有关更多详细信息,请参阅Creating a Custom Layout .

相关问题