c# - 在应用程序崩溃的 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项目上运行该项目。
有谁知道我做错了什么?谢谢你
解决方案
我认为您需要在内容页面中定义父布局。可能是一个<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>
推荐阅读
- git - git push操作第一次push到仓库报错
- python - PYTHON3:如何打印出用户输入字符串并用逗号分隔打印出来?
- ios - 无法本地化不匹配人脸的人脸 ID 弹出窗口
- shopify - 在 Shopify 中堆叠折扣代码
- algorithm - 在最小堆中找到第 7 个最小的元素
- numpy - Tensorflow 矩阵乘法比 numpy 慢
- php - Mysql从2个表中的2列完全搜索
- reinforced-typings - 如何在不创建无效导入的情况下引用外部库类型
- python-3.x - 如何使用 python 脚本将用户在我的网站上上传的文件存储到 azure 上?
- r - 如何在 R 中使用 smotefamily 处理分类变量?