无法在发布版本中设置特定平台的边距。

huangapple go评论90阅读模式
英文:

Cannot set Platform specific Margin in Release-Build

问题

在我的.NET MAUI应用程序中,我有以下Grid.Resources:

```xml
<Grid.Resources>
    <Style TargetType="Entry">
        <Setter Property="VerticalTextAlignment" Value="Center"/>
        <Setter Property="HorizontalTextAlignment" Value="Center"/>
        <Setter Property="Keyboard" Value="Numeric"/>
        <!-- <Setter Property="Margin" Value="{OnPlatform iOS='0, 0, 0, 0', Android='0, -5, 0, -3'}" /> -->
    </Style>
</Grid.Resources>

在iOS模拟器和Android上的Debug构建中,没有问题,这段代码可以编译通过。

但在Release构建中,用于在我的iPhone上部署时,我会收到一个编译错误,错误信息是"Margin-Setter没有值"。即使我删除iOS属性的值,因为它默认是如此,我仍然会收到此错误。

我可以做什么?


<details>
<summary>英文:</summary>

In my .NET MAUI App, I have the following Grid.Resources:

<Grid.Resources>
<Style TargetType="Entry">
<Setter Property="VerticalTextAlignment" Value="Center"/>
<Setter Property="HorizontalTextAlignment" Value="Center"/>
<Setter Property="Keyboard" Value="Numeric"/>
<!--<Setter Property="Margin" Value="{OnPlatform iOS='0, 0, 0, 0', Android='0, -5, 0, -3'}" />-->
</Style>
</Grid.Resources>



In **Debug**-Build on an iOS-Simulator and Android it is no problem and this code compiles without errors.

In **Releas-Build** to Deploy on my iPhone, I get a Compile-Error &quot;No Value for Setter&quot; on the Margin-Setter. Even when I delete the iOS-Property value as it is default anyway, I get this error.

What can I do?

</details>


# 答案1
**得分**: 1

我实际上喜欢在设置器中明确使用 `OnPlatforms` 的方法,甚至在我的项目中的发布模式下尝试过,它似乎总是有效的。

您可以尝试并查看它是否适用于您。

```xml
<Style TargetType="Entry">
    <Setter Property="VerticalTextAlignment" Value="Center" />
    <Setter Property="HorizontalTextAlignment" Value="Center" />
    <Setter Property="Keyboard" Value="Numeric" />
    <Setter Property="Margin">
        <Setter.Value>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="Android" Value="0, 0, 0, 0" />
                <On Platform="iOS" Value="0, 20, 0, 0" />
            </OnPlatform>
        </Setter.Value>
    </Setter>
</Style>

希望对您有所帮助。

英文:

I actually like to use an explicit approach to using OnPlatforms in setters and I even tried this on Release mode in my project and it always seems to work.

You can try it out and see if it works for you

     &lt;Style TargetType=&quot;Entry&quot;&gt;
            &lt;Setter Property=&quot;VerticalTextAlignment&quot; Value=&quot;Center&quot; /&gt;
            &lt;Setter Property=&quot;HorizontalTextAlignment&quot; Value=&quot;Center&quot; /&gt;
            &lt;Setter Property=&quot;Keyboard&quot; Value=&quot;Numeric&quot; /&gt;
            &lt;Setter Property=&quot;Margin&quot;&gt;
                &lt;Setter.Value&gt;
                    &lt;OnPlatform x:TypeArguments=&quot;Thickness&quot;&gt;
                        &lt;On Platform=&quot;Android&quot; Value=&quot;0, 0, 0, 0&quot; /&gt;
                        &lt;On Platform=&quot;iOS&quot; Value=&quot;0, 20, 0, 0&quot; /&gt;
                    &lt;/OnPlatform&gt;
                &lt;/Setter.Value&gt;
            &lt;/Setter&gt;
      &lt;/Style&gt;

huangapple
  • 本文由 发表于 2023年7月24日 00:45:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定