简体   繁体   English

Xamarin:重用效果

[英]Xamarin : Reuse an Effect

I have the following 我有以下

PlatformEffect Android Project PlatformEffect Android项目

public class LabelShadowEffect : PlatformEffect
{
   protected override void OnAttached()
   {
      ...
   }

   protected override void OnDetached()
   {
      ...
   }
}

RoutingEffect .NetStandard Lib RoutingEffect .NetStandard库

public class ShadowEffect : RoutingEffect
{
   ...
}

Xaml Xaml

<controls:FontAwesomeLabel

  ...

  <controls:FontAwesomeLabel.Effects>
     <effects:ShadowEffect
        DistanceX="2"
        DistanceY="2"
        Radius="2"
        Color="{StaticResource HeaderTextShadowColor}" />
  </controls:FontAwesomeLabel.Effects>
</controls:FontAwesomeLabel>

This works fine, though it would be nice to be able to reuse this as a static resource. 可以很好地工作,尽管能够将其作为静态资源重用会很好。 So, i notice a can set the property Element.Effects , its defined as follows 因此,我注意到可以设置属性Element.Effects ,其定义如下

public System.Collections.Generic.IList<Xamarin.Forms.Effect> Effects { get; }

I would have thought i could just write a xaml List or an array and put it in a resource and reuse it 我以为我可以写一个xaml List或一个数组并将其放入资源中并重用它

ie

Effects="{StaticResource StandardShadowEffect}"

Attempt 1 , rightclick and create the type, it produces an system:ArraySegment 尝试1 ,右键单击并创建类型,它将产生一个system:ArraySegment

<system:ArraySegment x:Key="StandardShadowEffect" x:TypeArguments="Effect">

</system:ArraySegment>

System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array System.ArrayTypeMismatchException:尝试以与数组不兼容的类型访问元素


Attempt 2 , List 尝试2 ,清单

 <generic:List x:Key="StandardShadowEffect" x:TypeArguments="Effect">

 </generic:List>

System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array System.ArrayTypeMismatchException:尝试以与数组不兼容的类型访问元素


Attempt 3 : Array 尝试3 :数组

<x:Array x:Key="StandardShadowEffect" x:TypeArguments="Effect">

</x:Array>

Compile error 编译错误

Expected Type IList 预期类型IList


Attempt 4 : Just specifying it and using it 尝试4 :仅指定它并使用它

<effects:ShadowEffect x:Key="StandardShadowEffect"
       DistanceX="2"
       DistanceY="2"
       Radius="2"
       Color="{StaticResource HeaderTextShadowColor}" />

Strangely enough this last attempt compiles and doesn't throw, yet no effect is applied 奇怪的是,最后一次尝试编译并且不会抛出,但是没有效果


Is there anyway to reuse an affect like this, without specifying it on every control? 无论如何,是否可以重用这样的效果,而不必在每个控件上都指定它? I'm starting to think effects "need" need to be implicitly specified per control 我开始认为需要为每个控件隐式指定效果“需要”

The documentation clearly states that: 文档明确指出:

An effect instance can only be attached to a single control. 效果实例只能附加到单个控件。 Therefore, an effect must be resolved twice to use it on two controls. 因此,必须对效果进行两次解析才能在两个控件上使用它。

So that should expected behaviour. 因此,这应该是预期的行为。 Maybe you can try to extend "needed" UI controls and add a new instance of effect in code behind. 也许您可以尝试扩展“需要的” UI控件,并在后面的代码中添加新的效果实例。 Keep in mind that if you do some endless animation in your effect you should remove the effect as well (tricky part). 请记住,如果您在效果中进行了无尽的动画处理,则也应删除效果(微妙的部分)。

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

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