Xaml Xamarin形成IOS按钮,看起来扭曲
我有以下XAML代码:Xaml Xamarin形成IOS按钮,看起来扭曲,xaml,xamarin.forms,xamarin.ios,Xaml,Xamarin.forms,Xamarin.ios,我有以下XAML代码: <StackLayout Grid.Row="2" Orientation="Horizontal" VerticalOptions="End" Margin="0,0,0,20" Spacing="28"> <Button x
<StackLayout
Grid.Row="2"
Orientation="Horizontal"
VerticalOptions="End"
Margin="0,0,0,20"
Spacing="28">
<Button
x:Name="SignInButton"
Visual="Material"
Padding="5"
Margin="10,0,0,0"
Style="{DynamicResource ButtonSecondary}"
HorizontalOptions="FillAndExpand"
Text="Sign In"
Clicked="SignInButton_Clicked"/>
<Button
x:Name="JoinUsButton"
Visual="Material"
Padding="5"
Margin="0,0,10,0"
Style="{DynamicResource ButtonPrimary}"
HorizontalOptions="FillAndExpand"
VerticalOptions="End"
Text="Join Us"
Clicked="JoinUsButton_Clicked"/>
</StackLayout>
当前存储在App.xaml文件中的动态资源如下所示:
然而,当我在iOS上运行应用程序时,按钮看起来像下图
但是,在android设备上,按钮看起来像下图:
虽然我看不出失真的确切问题,但我确实看到了平台之间的不一致。这最终归结为各个平台如何渲染CornerRadius属性。Android会将其限制在明显可见的范围内,基本上是高度/宽度的一半,以较小者为准,而iOS会按照你的要求执行 这张图显示了我当前看到的左侧,中间是我的第二个解决方案,右侧是我的第一个解决方案 我可能的解决办法是: 附加行为
虽然我看不出失真的确切问题,但我确实看到了平台之间的不一致。这最终归结为各个平台如何渲染CornerRadius属性。Android会将其限制在明显可见的范围内,基本上是高度/宽度的一半,以较小者为准,而iOS会按照你的要求执行 这张图显示了我当前看到的左侧,中间是我的第二个解决方案,右侧是我的第一个解决方案 我可能的解决办法是: 附加行为 Cauuse:在iOS中,如果你想达到上面安卓系统中的效果,你需要将CornerRadius设置为其HeightRequest的一半 解决方案 选择1 如果按钮的大小始终为固定值,则只需在样式中设置HeightRequest
<Style x:Name="ButtonSecondary" x:Key="ButtonSecondary" TargetType="Button" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor"
Value="{DynamicResource SecondaryColor}" />
<Setter Property="TextColor"
Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="BorderWidth"
Value="1" />
<Setter Property="BorderColor"
Value="{DynamicResource SecondaryBorderColor}" />
<Setter Property="CornerRadius"
Value="25" />
<Setter Property="HeightRequest"
Value="50" /> // double of CornerRadius
</Style>
在iOS中
在xaml中
Cauuse:在iOS中,如果你想达到上面安卓系统中的效果,你需要将CornerRadius设置为其HeightRequest的一半
解决方案
选择1
如果按钮的大小始终为固定值,则只需在样式中设置HeightRequest
<Style x:Name="ButtonSecondary" x:Key="ButtonSecondary" TargetType="Button" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor"
Value="{DynamicResource SecondaryColor}" />
<Setter Property="TextColor"
Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="BorderWidth"
Value="1" />
<Setter Property="BorderColor"
Value="{DynamicResource SecondaryBorderColor}" />
<Setter Property="CornerRadius"
Value="25" />
<Setter Property="HeightRequest"
Value="50" /> // double of CornerRadius
</Style>
在iOS中
在xaml中
我不知道为什么这个答案会被标记下来,我只是想把我的想法围绕在你的答案上answer@George我也是,我很感激我没有提供一个例子,但我现在添加了2个:我不知道为什么这个答案被标记下来,我只是想把我的想法围绕在你的答案上answer@George我也是,我很感激我没有提供示例,但我现在添加了2:@George你可以检查我的答案。@George你可以检查我的答案。它现在起作用了吗?它现在起作用了吗?
public class RoundedButton : Button
{
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
this.CornerRadius = (int)Math.Min(width, height) / 2;
}
}
<Style x:Name="ButtonSecondary" x:Key="ButtonSecondary" TargetType="Button" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor"
Value="{DynamicResource SecondaryColor}" />
<Setter Property="TextColor"
Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="BorderWidth"
Value="1" />
<Setter Property="BorderColor"
Value="{DynamicResource SecondaryBorderColor}" />
<Setter Property="CornerRadius"
Value="25" />
<Setter Property="HeightRequest"
Value="50" /> // double of CornerRadius
</Style>
public class MyButton:Button
{
}
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using App6;
using App6.iOS;
using System.ComponentModel;
[assembly:ExportRenderer(typeof(MyButton),typeof(MyButtonRenderer))]
namespace App6.iOS
{
public class MyButtonRenderer:ButtonRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(e.PropertyName=="Height")
{
var height = Element.Height;
Control.Layer.MasksToBounds = true;
Control.Layer.BorderColor = UIColor.Black.CGColor;
Control.Layer.CornerRadius = (nfloat)(height / 2.0);
Control.Layer.BorderWidth = (nfloat)0.5;
}
}
}
}
<local:MyButton
x:Name="SignInButton"
Visual="Material"
Padding="5"
Margin="10,0,0,0"
Style="{DynamicResource ButtonSecondary}"
HorizontalOptions="FillAndExpand"
Text="Sign In"
/>