简体   繁体   English

Xamarin Forms:带圆角的 StackLayout

[英]Xamarin Forms: StackLayout with rounded corners

I am developing an app using Xamarin Forms PCL.我正在使用 Xamarin Forms PCL 开发应用程序。 I need a StackLayout with rounded corners.我需要一个带圆角的 StackLayout。 I have tried frame as well for rounded corner container but there is no corner radius property available for it.我也为圆角容器尝试过框架,但没有可用的圆角半径属性。 I cannot find renderers for iOS,Android,UWP,Windows 8.1.我找不到适用于 iOS、Android、UWP、Windows 8.1 的渲染器。

Please can any one suggest me how to achieve StackLayout with rounded corners along with corner radius property for all the platforms.请任何人建议我如何实现带有圆角的 StackLayout 以及所有平台的圆角半径属性。 在此处输入图片说明

You can use Frame and put StackLayout inside , Note Frame take padding 20 by default :您可以使用 Frame 并将 StackLayout 放在里面,注意 Frame 默认采用 padding 20 :

<Frame CornerRadius="10"  
       OutlineColor="Red" 
       Padding="0">
            <StackLayout>

            </StackLayout>
</Frame>
<!--Curved stack-->
<Frame CornerRadius="5" 
           HorizontalOptions="Center" 
           VerticalOptions="Start"
           HasShadow="True"
           IsClippedToBounds="True"
           Padding="0">
        <StackLayout Padding="10,5,10,5"   
                         Orientation="Horizontal" 
                         BackgroundColor="White"  >
            <Image Source="settingsIcon"  
                   HeightRequest="25" 
                   WidthRequest="25" 
                   Aspect="Fill" />
            <Label Text="Filter" 
                   FontSize="Medium" 
                   VerticalTextAlignment="Center" 
                   VerticalOptions="Center"/>
        </StackLayout>
    </Frame>

I just tried to copy BigBasket's filter buttons.我只是试图复制 BigBasket 的过滤器按钮。 See How cool it looks看看它看起来多么酷

Since Xamarin has released Effects mechanism, it now can be done by implementing a custom effect on both platforms.由于 Xamarin 已经发布了Effects机制,现在可以通过在两个平台上实现自定义效果来完成。 An advantage of this approach is that effects are more light-weight, reusable and can be parameterized and applied to any UI element.这种方法的一个优点是效果更轻量级、可重用,并且可以参数化并应用于任何 UI 元素。

After you create a custom RoundCornersEffect inheriting RoutingEffect , declare a CornerRadius attached property and implement PlatformEffect on each platform, it can be applied to any Xamarin.Forms layout or control like this:创建继承RoutingEffect的自定义RoundCornersEffect 、声明CornerRadius附加属性并在每个平台上实现PlatformEffect ,它可以应用于任何Xamarin.Forms布局或控件,如下所示:

<StackLayout effects:RoundCornersEffect.CornerRadius="48"/>

with hardcoded corners radius or a value from resources带有硬编码的角半径或来自资源的值

 <BoxView effects:RoundCornersEffect.CornerRadius="{StaticResource LargeCornerRadius}" /> 

Here is a link to full implementation and usage examples .这是完整实现和使用示例的链接。

Just use a Frame with CornerRadius and set IsClippedToBounds to True.只需使用带有 CornerRadius 的 Frame 并将 IsClippedToBounds 设置为 True。 That should do the trick.这应该够了吧。

<Frame CornerRadius="30" 
           HorizontalOptions="Center" 
           VerticalOptions="Start"
           HasShadow="True"
           IsClippedToBounds="True"
           Padding="0">
<StackLayout></StackLayout>
</Frame>

I recently had the same need, so I created a Custom Renderer for both iOS and Android.我最近有同样的需求,所以我为 iOS 和 Android 创建了一个自定义渲染器。 I released it as a Nuget which you can find here .我将它作为 Nuget 发布,您可以在此处找到。 The source code is available on GitHub , and here is a little "How-To"源代码可在GitHub找到,这里有一些“操作方法”

Hope this helps!希望这可以帮助! It is very easy to use (Same as a ContentView, which it is at it's base), although note this is compile for .NET Standard, but you can also pull the code into your PCL它非常易于使用(与 ContentView 相同,它是它的基础),尽管请注意这是为 .NET Standard 编译的,但您也可以将代码拉入您的 PCL

A lot of valid answers were already given.已经给出了很多有效的答案。

I just wanted to add that since Xamarin Forms 5, Shapes control were added.我只想补充一点,自 Xamarin Forms 5 以来,添加了形状控件。

Now, you can just add a Rectangle which exposes RadiusX and RadiusY.现在,您只需添加一个暴露 RadiusX 和 RadiusY 的 Rectangle。

You can set rounded corner for any Layout or View or Cell (StackLayout, Grid, ListView)您可以为任何布局或视图或单元格(StackLayout、Grid、ListView)设置圆角

http://venkyxamarin.blogspot.in/2017/12/how-to-set-corner-radius-for-view.html#more http://venkyxamarin.blogspot.in/2017/12/how-to-set-corner-radius-for-view.html#more

Try using PancakeView Nuget Package.尝试使用 PancakeView Nuget 包。 First, install the package in your PCL project.首先,在您的 PCL 项目中安装该软件包。 Give the reference in xaml content page.在 xaml 内容页面中提供参考。

xmlns:pkView="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"

<StackLayout>
      <pkView:PancakeView>
            CornerRadius="10,0,10,0" 
      </pkView:PancakeView>
 </StackLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM