首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在应用程序处于中断模式时在xamarin窗体上创建自定义呈现会崩溃

在应用程序处于中断模式时在xamarin窗体上创建自定义呈现会崩溃
EN

Stack Overflow用户
提问于 2020-07-15 03:22:50
回答 2查看 227关注 0票数 0

我在https://www.youtube.com/watch?v=ux09gAB13kQ (感谢Houssem Dellai)上关注了为xamarin表单创建自定义呈现的视频:....the代码如下:

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

代码语言:javascript
运行
复制
public class RoundedEntry : Entry
{
}

在android解决方案中添加:

代码语言:javascript
运行
复制
 [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中添加:

代码语言:javascript
运行
复制
  [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:

代码语言:javascript
运行
复制
 <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加载时,项目崩溃,并显示错误:应用程序处于中断模式。

代码语言:javascript
运行
复制
public TestPage ()
        {
            InitializeComponent ();
        }

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

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

有人知道我做错了什么吗?谢谢

EN

回答 2

Stack Overflow用户

发布于 2020-07-15 10:44:34

我只需要使用你的代码,一切都在我这边运行得很好。我在Android和iOS端都测试了它。

我上传了我的示例项目here,您可以查看它。

如果你有任何问题,请随时问我。

结果如下:

票数 0
EN

Stack Overflow用户

发布于 2020-07-15 22:03:17

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

代码语言:javascript
运行
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62902564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档