【问题标题】:How to set different corner radius for Frame?如何为 Frame 设置不同的圆角半径?
【发布时间】:2019-01-18 10:41:59
【问题描述】:

为框架设置每个角半径很热门?可以一个一个地设置,但是我一个一个地设置,2四舍五入2正常。

【问题讨论】:

  • 你的尝试在哪里?
  • 默认的Frame 小部件不允许更改单个角,您当然可以通过每个平台的自定义渲染器来执行此操作。即在 iOS 上,您可以向控件添加遮罩层,stackoverflow.com/a/43877570/4984832

标签: c# xamarin xamarin.forms


【解决方案1】:

没有破解,有一个非常简单的解决方案:

使用 nuget 包Xamarin.Forms.PancakeView

如何使用?

  1. 安装 nuget 包 Xamarin.Forms.PancakeView。
  2. 那么你需要做的就是告诉我们的 XAML 页面在哪里可以找到 PancakeView,这是通过在我们的 ContentPage 中添加以下属性来完成的:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView">
       ...
</ContentPage>
  1. 只需将 PancakeView 打到该页面上即可:
<yummy:PancakeView BackgroundColor="#bc91d7" CornerRadius="60,0,0,60" IsClippedToBounds="true" HorizontalOptions="FillAndExpand" HeightRequest="150">
   <Image Source="unicorn.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" />
</yummy:PancakeView>

【讨论】:

  • 只是和仅供参考,根据该组件的发布者的说法,由于他们还有其他项目要处理,所以它正在被搁置。它已经无法在 UWP 应用中正常工作。
【解决方案2】:

对 xaml 的一次非常骇人听闻的尝试,您仍然可以注意到边界上的伪影,但为了快速开始,为什么不...

    <Grid>

        <Frame Margin="0,0,0,20" HasShadow="False" BorderColor="Transparent" CornerRadius="12" HorizontalOptions="Fill" VerticalOptions="Fill" BackgroundColor="Gray"/>
        <Frame Margin="1,1,1,20" HasShadow="False" BorderColor="Transparent" CornerRadius="11" HorizontalOptions="Fill" VerticalOptions="Fill" BackgroundColor="White"/>
        <Frame Margin="0,20,0,0" VerticalOptions="Fill" HasShadow="False" BorderColor="Transparent" CornerRadius="2" HorizontalOptions="Fill" BackgroundColor="Gray" />
        <Frame Margin="1,20,1,1" VerticalOptions="Fill"  HasShadow="False" BorderColor="Transparent" CornerRadius="2" HorizontalOptions="Fill" BackgroundColor="White" />
        <BoxView Margin="1.75,15,1.75,15" HorizontalOptions="Fill" VerticalOptions="Fill" BackgroundColor="White" HeightRequest="19" StyleId="hide background"/>

        <ContentView Margin="8" x:Name="MainContainer">
            <StackLayout >
                <Label TextColor="DimGray" Text="This is your main container"/>
                <Label TextColor="DimGray" Text="Put stuff here"/>
            </StackLayout>
        </ContentView>

    </Grid>

【讨论】:

    【解决方案3】:

    您需要在每个平台上创建自定义渲染器。我不知道你是否可以单独控制半径,但你可以控制拐角是否符合半径。

    首先,您需要在共享项目中使用一个自定义类来充当您的自定义控件...

    using System;
    using Xamarin.Forms;
    
    namespace MyApp.Controls
    {
        public class CustomFrame : Frame
        {
            // ---------------------------------------------------------------------------------------------------------------
            public static readonly BindableProperty CornerRadiusTopLeftProperty = BindableProperty.Create(
                propertyName: "CornerRadiusTopLeft",
                returnType: typeof(bool),
                declaringType: typeof(CustomFrame),
                defaultValue: true,
                defaultBindingMode: BindingMode.TwoWay
            );
    
            public bool CornerRadiusTopLeft
            {
                get { return (bool)GetValue(CornerRadiusTopLeftProperty); }
                set { base.SetValue(CornerRadiusTopLeftProperty, value); }
            }
    
            // ---------------------------------------------------------------------------------------------------------------
            public static readonly BindableProperty CornerRadiusTopRightProperty = BindableProperty.Create(
                propertyName: "CornerRadiusTopRight",
                returnType: typeof(bool),
                declaringType: typeof(CustomFrame),
                defaultValue: true,
                defaultBindingMode: BindingMode.TwoWay
            );
    
            public bool CornerRadiusTopRight
            {
                get { return (bool)GetValue(CornerRadiusTopRightProperty); }
                set { base.SetValue(CornerRadiusTopRightProperty, value); }
            }
    
            // ---------------------------------------------------------------------------------------------------------------
            public static readonly BindableProperty CornerRadiusBottomLeftProperty = BindableProperty.Create(
                propertyName: "CornerRadiusBottomLeft",
                returnType: typeof(bool),
                declaringType: typeof(CustomFrame),
                defaultValue: true,
                defaultBindingMode: BindingMode.TwoWay
            );
    
            public bool CornerRadiusBottomLeft
            {
                get { return (bool)GetValue(CornerRadiusBottomLeftProperty); }
                set { base.SetValue(CornerRadiusBottomLeftProperty, value); }
            }
    
            // ---------------------------------------------------------------------------------------------------------------
            public static readonly BindableProperty CornerRadiusBottomRightProperty = BindableProperty.Create(
                propertyName: "CornerRadiusBottomRight",
                returnType: typeof(bool),
                declaringType: typeof(CustomFrame),
                defaultValue: true,
                defaultBindingMode: BindingMode.TwoWay
            );
    
            public bool CornerRadiusBottomRight
            {
                get { return (bool)GetValue(CornerRadiusBottomRightProperty); }
                set { base.SetValue(CornerRadiusBottomRightProperty, value); }
            }
        }
    }
    

    然后您需要在每个平台上创建渲染器。我还没有完成 Android 端,但这是你需要的 iOS ...

    using System;
    
    using CoreAnimation;
    
    using MyApp.iOS.CustomRenderers;
    using Foundation;
    using MyApp.Controls;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(CustomFrame), typeof(CustomFrameRenderer))]
    namespace MyApp.iOS.CustomRenderers
    {
        public class CustomFrameRenderer : FrameRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
            {
                base.OnElementChanged(e);
    
                if (Element != null)
                {
                    var element = Element as CustomFrame;
    
                    int result = 0;
    
                    if (element.CornerRadiusTopLeft)
                        result += (int)CACornerMask.MinXMinYCorner;
    
                    if (element.CornerRadiusTopRight)
                        result += (int)CACornerMask.MaxXMinYCorner;
    
                    if (element.CornerRadiusBottomLeft)
                        result += (int)CACornerMask.MinXMaxYCorner;
    
                    if (element.CornerRadiusBottomRight)
                        result += (int)CACornerMask.MaxXMaxYCorner;
    
                    Layer.MaskedCorners = (CACornerMask)result;
                };
            }
        }
    }
    

    然后,您可以在 XAML 文件中将其用作自定义控件。

    将命名空间添加到您的页面...

    xmlns:customControls="clr-namespace:MyApp.Controls"
    

    ...然后添加您的自定义框架...

    <customControls:CustomFrame BackgroundColor="White" CornerRadius="10" HasShadow="false"            
            CornerRadiusBottomLeft="false" CornerRadiusBottomRight="false" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Padding="15">
    
        <!-- Add your other elements here -->
    </customControls:CustomFrame>
    

    希望对你有所帮助。祝 Android 渲染器好运,我相信这并不难,只是还没开始。

    【讨论】:

      【解决方案4】:

      使用 Xamarin.Forms.PancakeView。

       <yummy:PancakeView BackgroundColor="#2DA6EA"  WidthRequest="300" HorizontalOptions="Start" CornerRadius="0,30,0,30">
                          <StackLayout>
                          <Label TextColor="White" Margin="10,0,0,0" FontSize="15" Text="Welcome"/>
                          <Label TextColor="White" Margin="10,0,0,0"  FontSize="20" Text="Dolapo"/>
                      </StackLayout>
                      </yummy:PancakeView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多