英文:
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 "No Value for Setter" 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
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论