I can't figure out why my Label isn't triggering the converter when inside of span, but does when it isn't? What am I missing here?

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

I can't figure out why my Label isn't triggering the converter when inside of span, but does when it isn't? What am I missing here?

问题

我有两个相同数据的标签示例。一个可以工作,但不符合我的要求(它根据社区名称而不是社区名称和用户名的不同颜色对所有文本进行着色),另一个可以工作,但不触发转换器(所以文本显示正常,但不通过转换器进行着色)。我认为第二个不触发转换器的标签是我想要的。

工作的标签,但不符合要求

<Label 
    x:Name="communityAndUserLabel"
    Grid.Row="2"
    Grid.Column="1"
    FontAttributes="None"
    VerticalOptions="End">
    <Label.Text>
        <MultiBinding StringFormat="{}{0} • {1}" Mode="TwoWay">
            <Binding Path="CommunityName" />
            <Binding Path="Username" />
        </MultiBinding>
    </Label.Text>
    <Label.TextColor>
        <MultiBinding Converter="{StaticResource nameColorMulti}" ConverterParameter="community">
            <Binding Path="CommunityName" />
            <Binding Path="Username" />
        </MultiBinding>
    </Label.TextColor>
</Label>

不触发转换器的标签,但仍显示文本

<Label 
    x:Name="communityAndUserLabel"
    Grid.Row="2"
    Grid.Column="1"
    FontAttributes="None"
    VerticalOptions="End">
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{Binding CommunityName, Mode=TwoWay}" TextColor="{Binding CommunityName, Converter={StaticResource nameColorMulti}, ConverterParameter=community}" />
            <Span Text=" • " TextColor="Gray" />
            <Span Text="{Binding Username, Mode=TwoWay}" TextColor="{Binding Username, Converter={StaticResource nameColorMulti}, ConverterParameter=username}" />
        </FormattedString>
    </Label.FormattedText>
</Label>

你有没有想到为什么第二个标签不触发转换器的原因?

英文:

I have two Label examples for the same data. One works - but doesn't work like I want it to (it colorized all of the text based on the community name rather than both the community name and the username being different colors), the other works - but doesn't trigger the converter to fire (so the text displays as expected, but doesn't run through the converter for colorization). I think the second one that doesn't trigger the convertor is what I'm after.

Working Label, but doesn't do what I want

&lt;Label 
    x:Name=&quot;communityAndUserLabel&quot;
    Grid.Row=&quot;2&quot;
    Grid.Column=&quot;1&quot;
    FontAttributes=&quot;None&quot;
    VerticalOptions=&quot;End&quot;&gt;
    &lt;Label.Text&gt;
        &lt;MultiBinding StringFormat=&quot;{}{0} • {1}&quot; Mode=&quot;TwoWay&quot;&gt;
            &lt;Binding Path=&quot;CommunityName&quot; /&gt;
            &lt;Binding Path=&quot;Username&quot; /&gt;
        &lt;/MultiBinding&gt;
    &lt;/Label.Text&gt;
    &lt;Label.TextColor&gt;
        &lt;MultiBinding Converter=&quot;{StaticResource nameColorMulti}&quot; ConverterParameter=&quot;community&quot;&gt;
            &lt;Binding Path=&quot;CommunityName&quot; /&gt;
            &lt;Binding Path=&quot;Username&quot; /&gt;
        &lt;/MultiBinding&gt;
    &lt;/Label.TextColor&gt;
&lt;/Label&gt;

Label that won't trigger the converter, but still shows the text

&lt;Label 
    x:Name=&quot;communityAndUserLabel&quot;
    Grid.Row=&quot;2&quot;
    Grid.Column=&quot;1&quot;
    FontAttributes=&quot;None&quot;
    VerticalOptions=&quot;End&quot;&gt;
    &lt;Label.FormattedText&gt;
        &lt;FormattedString&gt;
            &lt;Span Text=&quot;{Binding CommunityName, Mode=TwoWay}&quot; TextColor=&quot;{Binding CommunityName, Converter={StaticResource nameColorMulti}, ConverterParameter=community}&quot; /&gt;
            &lt;Span Text=&quot; • &quot; TextColor=&quot;Gray&quot; /&gt;
            &lt;Span Text=&quot;{Binding Username, Mode=TwoWay}&quot; TextColor=&quot;{Binding Username, Converter={StaticResource nameColorMulti}, ConverterParameter=username}&quot; /&gt;
        &lt;/FormattedString&gt;
    &lt;/Label.FormattedText&gt;
&lt;/Label&gt;

Any idea on what I'm overlooking here on why the second one doesn't trigger the converter?

答案1

得分: 1

今天早上我突然意识到,由于我在xaml中将文本分开,我不再需要使用IMultiValueConverter了,所以我创建了两个单独的IValueConverter来完成这个任务:

&lt;Label 
    x:Name=&quot;communityAndUserLabel&quot;
    Grid.Row=&quot;2&quot;
    Grid.Column=&quot;1&quot;
    FontAttributes=&quot;None&quot;
    VerticalOptions=&quot;End&quot;&gt;
    &lt;Label.FormattedText&gt;
        &lt;FormattedString&gt;
            &lt;!-- 使用带有Converter属性的绑定标记扩展 --&gt;
            &lt;Span Text=&quot;{Binding CommunityName}&quot; TextColor=&quot;{Binding CommunityName, Converter={StaticResource communityNameColor}}&quot; /&gt;
            &lt;Span Text=&quot; • &quot; TextColor=&quot;Grey&quot; /&gt;
            &lt;Span Text=&quot;{Binding Username}&quot; TextColor=&quot;{Binding Username, Converter={StaticResource usernameColor}}&quot; /&gt;
        &lt;/FormattedString&gt;
    &lt;/Label.FormattedText&gt;
&lt;/Label&gt;
英文:

It dawned on me this morning that since I had separated the text out in the xaml I no longer needed to use a IMultiValueConverter - so instead I created two separate IValueConverters to get this done:

&lt;Label 
    x:Name=&quot;communityAndUserLabel&quot;
    Grid.Row=&quot;2&quot;
    Grid.Column=&quot;1&quot;
    FontAttributes=&quot;None&quot;
    VerticalOptions=&quot;End&quot;&gt;
    &lt;Label.FormattedText&gt;
        &lt;FormattedString&gt;
            &lt;!-- Use Binding markup extensions with Converter attributes --&gt;
            &lt;Span Text=&quot;{Binding CommunityName}&quot; TextColor=&quot;{Binding CommunityName, Converter={StaticResource communityNameColor}}&quot; /&gt;
            &lt;Span Text=&quot; • &quot; TextColor=&quot;Grey&quot; /&gt;
            &lt;Span Text=&quot;{Binding Username}&quot; TextColor=&quot;{Binding Username, Converter={StaticResource usernameColor}}&quot; /&gt;
        &lt;/FormattedString&gt;
    &lt;/Label.FormattedText&gt;
&lt;/Label&gt;

huangapple
  • 本文由 发表于 2023年8月9日 04:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862899.html
匿名

发表评论

匿名网友

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

确定