“Exception has been thrown by the target of an invocation in .Net Maui 7 Style”

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

Exception has been thrown by the target of an invocation in .Net Maui 7 Style

问题

我有一个自己开发的控件,其中有一个属性如下:

public TextAlignment HorizontalIconAlignment { get { return (TextAlignment)GetValue(HorizontalIconAlignmentProperty); } set { SetValue(HorizontalIconAlignmentProperty, value); } }
public static readonly BindableProperty HorizontalIconAlignmentProperty = BindableProperty.Create("HorizontalTextAlignment", typeof(TextAlignment), typeof(DuoToneIcon), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);
private static void OnHorizontalIconAlignmentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
    var control = (myControl)bindable;
    control.Label.HorizontalTextAlignment = (TextAlignment)newValue;
}

我可以如下正常使用它:

<local:myControl Text="Something" HorizontalIconAlignment="Center" PrimaryColor="red" />

问题是,我不想在整个软件中重复这个属性,因此我将它添加为样式在我的 Style.xaml 中如下:

<Style TargetType="local:myControl">
    <Setter Property="PrimaryColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}" />
</Style>

它运行得很完美,但是当我像下面这样更改并添加 HorizontalIconAlignment 时,我收到一个异常:

<Style TargetType="local:myControl">
    <Setter Property="PrimaryColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}" />
    <Setter Property="HorizontalIconAlignment" Value="Center" />
</Style>

我做错了什么?

异常信息:

Microsoft.Maui.Controls.Xaml.XamlParseException: '位置 10:37。类型转换器失败:目标调用引发了异常。'

XmlInfo: Resources/Styles/Styles.xaml

英文:

I have self-developed control which has one property as below:

public TextAlignment HorizontalIconAlignment { get { return (TextAlignment)GetValue(HorizontalIconAlignmentProperty); } set { SetValue(HorizontalIconAlignmentProperty, value); } }
public static readonly BindableProperty HorizontalIconAlignmentProperty = BindableProperty.Create(&quot;HorizontalTextAlignment&quot;, typeof(TextAlignment), typeof(DuoToneIcon), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);
private static void OnHorizontalIconAlignmentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
    var control = (myControl)bindable;
    control.Label.HorizontalTextAlignment = (TextAlignment)newValue;
}

I can use it very normally as below:

&lt;local:myControl Text=&quot;Something&quot; HorizontalIconAlignment=&quot;Center&quot; PrimaryColor=&quot;red&quot; /&gt;

The problem is, I don't want to repeat this property in the whole software, therefore I added it as style in my Style.xaml as bellow:

&lt;Style TargetType=&quot;local:myControl&quot;&gt;
    &lt;Setter Property=&quot;PrimaryColor&quot; Value=&quot;{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}&quot; /&gt;
&lt;/Style&gt;

And it's working perfect, but when I change it as bellow and added HorizontalIconAlignment then I receive an Exception.

&lt;Style TargetType=&quot;local:myControl&quot;&gt;
    &lt;Setter Property=&quot;PrimaryColor&quot; Value=&quot;{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}&quot; /&gt;
    &lt;Setter Property=&quot;HorizontalIconAlignment&quot; Value=&quot;Center&quot; /&gt;
&lt;/Style&gt;

Did I do something wrong?

Eception:

> Microsoft.Maui.Controls.Xaml.XamlParseException: 'Position 10:37. Type
> converter failed: Exception has been thrown by the target of an
> invocation.'
>
> XmlInfo: Resources/Styles/Styles.xaml

答案1

得分: 1

代码部分已翻译如下:

public static readonly BindableProperty HorizontalIconAlignmentProperty = 
      BindableProperty.Create("HorizontalIconAlignment", typeof(TextAlignment), typeof(DuoToneIcon), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);

应该是BindableProperty.Create("HorizontalIconAlignment"而不是HorizontalTextAlignment。BindableProperty.Create中的属性名应该与您声明的属性相同。

此外,第二个typeof应该是控件类型。因此,它应该如下所示:

public static readonly BindableProperty HorizontalIconAlignmentProperty = 
      BindableProperty.Create("HorizontalIconAlignment", typeof(TextAlignment), 
           typeof(myControl), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);

更多信息,请参考创建可绑定属性的官方文档。

英文:

In you code:

public static readonly BindableProperty HorizontalIconAlignmentProperty = 
      BindableProperty.Create(&quot;HorizontalTextAlignment&quot;, typeof(TextAlignment), typeof(DuoToneIcon), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);

It should be BindableProperty.Create(&quot;HorizontalIconAlignment&quot; not HorizontalTextAlignment. The property name in the BindableProperty.Create shoule be same as the property you delcared.

In addition, the second typeof should be the control. So it should be such as:

public static readonly BindableProperty HorizontalIconAlignmentProperty = 
      BindableProperty.Create(&quot;HorizontalIconAlignment&quot;, typeof(TextAlignment), 
           typeof(myControl), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);

For more information, you can refert to the official document about creating a bindable property

答案2

得分: 0

Sure, here is the translated code part without the HTML entities:

<Style TargetType="local:myControl">
    <Setter Property="PrimaryColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}" />
    <Setter Property="HorizontalIconAlignment" Value="{AppThemeBinding Center}" />
</Style>
I think the binding is missing you can try to add this binding as an example.
英文:
    &lt;Style TargetType=&quot;local:myControl&quot;&gt;
        &lt;Setter Property=&quot;PrimaryColor&quot; Value=&quot;{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}&quot; /&gt;
        &lt;Setter Property=&quot;HorizontalIconAlignment&quot; Value=&quot;{AppThemeBinding Center}&quot; /&gt;
    &lt;/Style&gt;

I think the binding is missing you can try to add this binding as example .

huangapple
  • 本文由 发表于 2023年3月12日 19:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712722.html
匿名

发表评论

匿名网友

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

确定