【问题标题】:Custom MaterialFrame control with only one platform renderer只有一个平台渲染器的自定义 MaterialFrame 控件
【发布时间】:2020-06-26 14:20:45
【问题描述】:

在我的 Xamarin.Forms 应用中,我有 MaterialFrame 自定义控件。

iOS 渲染器效果很好,看起来像:

public class MaterialFrameRenderer : FrameRenderer
{
    private const int ShadowColor = 0x939393;

    public override void Draw(
        CGRect rect)
    {
        base.Draw(rect);

        // Update shadow to match better material design standards of elevation
        Layer.ShadowRadius = Layer.CornerRadius;
        Layer.ShadowColor = ColorHelper.FromHex(ShadowColor).CGColor;
        Layer.ShadowOffset = new CGSize(1, 1);
        Layer.ShadowOpacity = 0.30f;
        Layer.ShadowPath = UIBezierPath.FromRect(Layer.Bounds).CGPath;
        Layer.MasksToBounds = false;
    }
}

在 Android 平台上,我想使用基于我的其他跨平台控件的实现。我的 .net 标准(共享项目)实现:

public class MaterialFrame : Frame
{
    public MaterialFrame()
    {
        if (Device.RuntimePlatform == Device.Android)
        {
            Content = new MyOtherCustomControl
            {
                BackgroundColor = Color.Red
            };
        }
    }
}

很遗憾,此实现不适用于 Android。你有什么建议吗?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    我创建了MyOtherCustomControl

    <StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="MyOtherCustomControl"
            VerticalOptions="CenterAndExpand" />
        <Button />
    </StackLayout> 
    

    并在MaterialFrame自定义控件中使用。

    public class MaterialFrame : Frame
    {
        public MaterialFrame()
        {
            if (Device.RuntimePlatform == Device.Android)
            {
                Content = new MyOtherCustomControl
                {
                    BackgroundColor = Color.Red
                };
            }
        }
    
    }
    

    MaterialFrame的使用:

    <ContentPage.Content>
        <StackLayout>
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" />
            <pages:MaterialFrame />
        </StackLayout>
    </ContentPage.Content>
    

    截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2013-05-24
      相关资源
      最近更新 更多