簡體   English   中英

如何使用xamarin表格為Windows 8.1(無Silverlight)手機圓角矩形邊緣

[英]How to round rectangle edges for a windows 8.1 (no silverlight) phone using xamarin forms

我正在使用Xamarin表單BoxView控件,並希望在Windows Phone 8.1(沒有silverlight)的邊緣圓。 為此我在windows phone項目中渲染控件並設置半徑,但它似乎沒有做任何事情。 下面是我正在使用的渲染代碼:

[assembly: ExportRenderer(typeof(RoundedBox), typeof(RoundedBoxRenderer))]

namespace MyProject.WinPhone.Renderer
{
    public class RoundedBoxRenderer : BoxViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
        {
            base.OnElementChanged(e);

            var boxRenderer = e.NewElement;
            RoundedBox rb = (RoundedBox)this.Element;

            if (this.Control != null)
            {
                var boxStyle = new Style(typeof(RoundedBox))
                {
                    Setters = {
                        new Setter {Property = RoundedBox.BackgroundColorProperty, Value = rb.BackgroundColor}
                    }
                };

                SetRoundedBoxRadius();

                boxRenderer.Style = boxStyle;
            }
        }

        private void SetRoundedBoxRadius(double radius)
        {
            ((Windows.UI.Xaml.Shapes.Rectangle)this.Control).RadiusX = 50;
        }
    }
}

RoundedBox 是我在PCL項目中生成的控件,它繼承自BoxView

根據我的發現,我不知道我做錯了什么,因為Xamarin根據以下內容將BoxView轉換為Windows Phone中的矩形形狀:

https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/renderers/

此形狀具有RadiusX和RadiusY屬性來設置邊框半徑:

https://msdn.microsoft.com/library/windows/apps/br243371

關於我缺少什么的任何想法? 謝謝!

您是否嘗試設置矩形的筆觸(顏色)和StrockThickness 根據你的代碼,那些沒有設置。 因此,您的矩形實際上沒有邊框。

您好,只需更新您的SetRoundedBoxRadius函數,如下所述。 你的代碼會起作用。

private void SetRoundedBoxRadius()
        {
            ((Windows.UI.Xaml.Shapes.Rectangle)this.Control).RadiusX = 50;
            ((Windows.UI.Xaml.Shapes.Rectangle)this.Control).RadiusY = 50;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM