简体   繁体   English

如何使用Device.OnPlatform翻译此填充? (iOS案例)

[英]How to translate this Padding with Device.OnPlatform? (iOS cases)

So Im working myself trough a xamarin.forms book right now and I came across this: 因此,我现在正通过一本xamarin.forms书来工作,我发现了:

 Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5);

Device.OnPlatform get´sa warning that it is obsolete. Device.OnPlatform得到一个过时的警告。

I know that I could translate 我知道我可以翻译

Padding = Device.OnPlatform(new Thickness(0,20,0,0),
new Thickness(0),
new Thickness(0));

to

        switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                Padding = new Thickness(0, 20, 0, 0);
                break;
        };

But what exactly happens here and how do I translate this? 但是这里到底发生了什么,我该如何翻译?

Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5);

The new Thickness(5, 5, 5, 5); new Thickness(5, 5, 5, 5); with 4 double parameters means that it will create a new padding (in this case) with 5 on the left, 5 on the top, 5 on the right and 5 on the bottom. 具有4个double参数意味着将创建一个新的填充(在这种情况下),左侧为5,顶部为5,右侧为5,底部为5。 So, from left to right, you just go clockwise and start from the left. 因此,从左到右,您只需沿顺时针方向从左开始。

We see that the second parameter (so the top padding) will be different depending on the platform. 我们看到第二个参数(所以顶部填充)将根据平台而有所不同。 From the top of my head, I am not sure what parameter is what platform, but I'm guessing the first one will be iOS. 从我的头上,我不确定哪个参数是哪个平台,但是我猜第一个将是iOS。 What happens now, is that the left will have a padding of 5 and the top padding will be different per platform. 现在发生的是,每个平台的左侧填充为5,顶部填充为不同。 For iOS, it will be 20 and for Android and UWP (remember, just a guess) it will still be 5. Then the right and bottom padding didn't change, so they are still 5 as well. 对于iOS,它将是20,对于Android和UWP(请记住,只是一个猜测),它将仍然是5。然后右侧和底部的填充没有变化,因此它们仍然是5。

If you want to do it strictly in code, it might translate to something like this: 如果要严格在代码中执行此操作,则可能会转换为以下内容:

double topPadding = 5;

switch (Device.RuntimePlatform)
{
    case Device.iOS:
        topPadding = 20;
        break;
    default:
        topPadding = 5;
        break;
};

Padding = new Thickness(5, topPadding, 5, 5);

Of course, you can add some different cases or change the values, totally up to you. 当然,您可以添加一些不同的大小写或更改值,完全由您决定。

You can also do it with XAML. 您也可以使用XAML。 It could then look like this: 然后可能看起来像这样:

<YourControl Padding="{OnPlatform '5,5,5,5', iOS='5,20,5,5'}" />

YourControl is the control you want to apply this on. YourControl是要对其应用此控件的控件。 The first '5,5,5,5' value after OnPlatform is a default value for all platforms not specified. 对于未指定的所有平台, OnPlatform之后的第一个'5,5,5,5'值是默认值。 The iOS is specified and will thus use the value specified for it. 指定了iOS,因此将使用为其指定的值。 Read more on the OnPlatform extension here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/markup-extensions/consuming#onplatform-markup-extension 在此处阅读有关OnPlatform扩展的更多信息: https ://docs.microsoft.com/zh-cn/xamarin/xamarin-forms/xaml/markup-extensions/consumption#onplatform-markup-extension

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

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