【发布时间】:2016-08-30 18:17:50
【问题描述】:
我在继承自 Application ResourceDictionary 中定义的隐式样式时遇到了一些问题。我尝试了几种不同的方法,但每种方法都以一种或另一种形式不成功。
尝试1:继承隐式样式
应用程序资源字典
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="#1386F1" />
<Setter Property="TextColor" Value="White" />
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="HeightRequest">
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="50" />
</Setter>
<Style.Triggers>
<Trigger TargetType="Button" Property="IsEnabled" Value="False">
<Setter Property="BackgroundColor">
<OnPlatform x:TypeArguments="Color" iOS="#E8E8E8" />
</Setter>
</Trigger>
</Style.Triggers>
</Style>
页面资源字典
<Style x:Key="LoginButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="BackgroundColor" Value="#024886" />
<Setter Property="Margin">
<OnPlatform x:TypeArguments="Thickness" iOS="0,5" />
</Setter>
</Style>
页面控制
<Button Style="{StaticResource LoginButtonStyle}" />
结果
抛出异常“找不到键 {x:Type Button} 的静态资源”。这就是我记得在 WPF 中执行此操作的方式,但我假设 Xamarin XAML 不支持此操作。
尝试 2:继承通用显式样式
应用程序资源字典
<Style x:Key="DefaultButtonStyle" TargetType="Button">
<!-- Same as Attempt 1 -->
</Style>
<!-- This implicit style causes issues with the inheritence of the Trigger in the above explicit style. When it's commented out, everything works fine. -->
<Style TargetType="Button" BasedOn="{StaticResource DefaultButtonStyle}" />
页面资源字典
<Style x:Key="LoginButtonStyle" TargetType="Button" BasedOn="{StaticResource DefaultButtonStyle}">
<!-- Same as Attempt 1 -->
</Style>
页面控制
<!-- Same as Attempt 1 -->
结果
除触发器外,一切正常。但是,当我删除隐式样式时,触发器会神奇地再次开始工作。不过,我宁愿不必在每个 Button 上指定显式样式。我相信这只是一个错误,但我想在花时间准备错误报告之前确定一下。
有什么想法吗?
【问题讨论】:
-
嗯,在 android 上,触发器正在工作。现在无法在 iOS 上验证它。作为一种解决方法(在每个按钮上声明触发器之前),您可以尝试使用行为。 developer.xamarin.com/guides/xamarin-forms/behaviors/creating
-
Android 上禁用按钮的默认背景已经是我在 iOS (#E8E8E8) 触发器中指定的颜色,所以我不确定它是否在那里工作。由于我仅将触发器应用于 iOS,因此我尚未测试它是否适用于 Android。
-
我把它改成了洋红色,它也能正常工作:)
-
如果您阅读源代码,您会看到它已实现。所以也许这是一个错误。 github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/…
-
尝试 3:您是否尝试在代码中定义触发动作?
标签: xaml inheritance xamarin xamarin.forms