首页 > 解决方案 > 在应用程序崩溃的 xamarin 表单上创建自定义渲染处于中断模式

问题描述

我已经关注了有关为 xamarin 表单创建自定义渲染的视频:https ://www.youtube.com/watch?v= ux09gAB13kQ(感谢 Houssem Dellai)......代码如下:

在主项目解决方案中添加一个类

public class RoundedEntry : Entry
{
}

在android解决方案中添加:

 [assembly: ExportRenderer(typeof(RoundedEntry), typeof(RoundedEntryRendererAndroid))]
namespace Project.Droid
{
    public class RoundedEntryRendererAndroid : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(e.OldElement == null)
            {
                //Use this code if want to use button from XAML page
               // Control.SetBackgroundResource(Resource.Layout.RoundedShape);

                
                //Use this button if you want to create button from c#
            var gradientDrawable = new GradientDrawable();
            gradientDrawable.SetCornerRadius(60f);
            gradientDrawable.SetStroke(5, Android.Graphics.Color.DeepPink);
            gradientDrawable.SetColor(Android.Graphics.Color.LightGray);
            Control.SetBackground(gradientDrawable);

            Control.SetPadding(50, Control.PaddingTop, Control.PaddingRight,
               Control.PaddingBottom);

            }
        }

        public RoundedEntryRendererAndroid()
           : base(null)
        {
            // Default constructor needed for Xamarin Forms bug?
            throw new Exception("This constructor should not actually ever be used");
        }
    }
}

并在 IOS 添加:

  [assembly: ExportRenderer(typeof(RoundedEntry), typeof(RoundedEntryRendererIos))]
namespace Project.iOS
{
    public class RoundedEntryRendererIos : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(e.OldElement == null)
            {
                Control.Layer.CornerRadius = 20;
                Control.Layer.BorderWidth = 3f;
                Control.Layer.BorderColor = Color.DeepPink.ToCGColor();
                Control.Layer.BackgroundColor = Color.LightGray.ToCGColor();

                Control.LeftView = new UIKit.UIView(new CGRect(0, 0, 10, 0));
                Control.LeftViewMode = UIKit.UITextFieldViewMode.Always;
            }
        }
    }
}

然后,当按下按钮时,使用自定义渲染加载 testPage:

 <Button Text="Order" Clicked="ProceedToCheckout" HorizontalOptions="End"></Button>

void ProceedToCheckout(object sender, EventArgs e)
        {
              Application.Current.MainPage = new NavigationPage(new TestPage());
        }


<?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:Lagans"
             x:Class="Lagans.Views.TestPage">
    
        <local:RoundedEntry></local:RoundedEntry>


</ContentPage>

namespace Projects.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestPage : ContentPage
    {
        public TestPage ()
        {
            InitializeComponent ();
        }
    }
}

但是,当 testpage 加载时,项目崩溃并出现错误:应用程序处于中断模式。

public TestPage ()
        {
            InitializeComponent ();
        }

被一个断点击中。但后来它崩溃了

我正在android项目上运行该项目。

有谁知道我做错了什么?谢谢你

标签: c#xamarinxamarin.formscustom-renderer

解决方案


我认为您需要在内容页面中定义父布局。可能是一个<Stacklayout>可以很好地工作。

<?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:Lagans"
             x:Class="Lagans.Views.TestPage">
    <Stacklayout>
        <local:RoundedEntry></local:RoundedEntry>
    </Stacklayout>

</ContentPage>

推荐阅读