英文:
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("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;
}
I can use it very normally as below:
<local:myControl Text="Something" HorizontalIconAlignment="Center" PrimaryColor="red" />
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:
<Style TargetType="local:myControl">
<Setter Property="PrimaryColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}" />
</Style>
And it's working perfect, but when I change it as bellow and added HorizontalIconAlignment then I receive an Exception.
<Style TargetType="local:myControl">
<Setter Property="PrimaryColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray900}}" />
<Setter Property="HorizontalIconAlignment" Value="Center" />
</Style>
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("HorizontalTextAlignment", typeof(TextAlignment), typeof(DuoToneIcon), TextAlignment.Start, propertyChanged: OnHorizontalIconAlignmentPropertyChanged);
It should be BindableProperty.Create("HorizontalIconAlignment"
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("HorizontalIconAlignment", 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.
英文:
<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 example .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论