如何在Xamarin.Forms中连接字符串?

17
我希望我的两个字符串能够显示在同一行。是否有可能它出现像这样:Curry Stephen,使用以下代码 Text="{Binding EMP_LAST_NAME + EMP_FIRST_NAME}"?我目前拥有的是这段代码。非常感谢。
<ListView ItemsSource="{Binding EmployeesList}"
        HasUnevenRows="True">
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <Grid Padding="10" RowSpacing="10" ColumnSpacing="5">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <controls:CircleImage Source="icon.png"
               HeightRequest="66"
               HorizontalOptions="CenterAndExpand"
               Aspect="AspectFill"
               WidthRequest="66"
               Grid.RowSpan="2"
               />

        <Label Grid.Column="1"
              Grid.Row="1"
              Text="{Binding EMP_LAST_NAME}"
               TextColor="White"
               FontSize="18"
               Opacity="0.6"/>

        <Label Grid.Column="1"
              Grid.Row="1"
              Text="{Binding EMP_FIRST_NAME}"
               TextColor="White"
               FontSize="18"
               Opacity="0.6"/>



      </Grid>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

->

5个回答

31

你不能在一个 View Element 上绑定多个属性。

在这种情况下,您应该创建一个新的属性,以满足您想要的格式,并将其绑定到 View

示例:

public class EmployeeViewModel
{
    public string FirstName { get; set; }    
    public string LastName { get; set; }    
    public string FullName => $"{FirstName} {LastName}";
}

接着在 XAML 中:

<Label Text="{Binding FullName}"/>

另一种方法:

正如评论中所建议的那样,我们还可以在Label中使用FormattedText属性:

<Label.FormattedText>
   <FormattedString>
     <Span Text="{Binding FirstName}" />
     <Span Text="{Binding LastName}"/>
   </FormattedString>
</Label.FormattedText>

8
使用Xamarin Forms中的FormattedText属性,可以在Label控件中实现如下效果:
<Label Grid.Column="1" Grid.Row="1">
    <Label.FormattedText>
        <FormattedString>
            <Span TextColor="White" FontSize="18" Text="{Binding EMP_LAST_NAME'}"/>
            <Span TextColor="White" FontSize="18" Text="{Binding EMP_FIRST_NAME}"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

Xamarin.Forms 标签

此外,您还可以添加 Style 以避免在代码中重复使用 TextColorFontSize 和其他属性。

使用 XAML 样式为 Xamarin.Forms 应用程序设置样式


6
 <Label Text="{Binding Value, StringFormat='string before value {0:F0} string after value'}"/>

假设您的值为1234。 在这种情况下,输出将如下所示:

值之前的字符串 1234 值之后的字符串


4

您可以使用 IValueConverter,它将接受 Employee 并返回完整姓名。

或者您可以使用 MultiComponentLabel。它允许您将不同的值绑定到一个 Label 上。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Name="Page"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:SuperForms.Controls;assembly=SuperForms.Controls"
             x:Class="SuperForms.Samples.MultiComponentLabelPage">
  <controls:MultiComponentLabel Margin="0,20,0,0">
    <controls:MultiComponentLabel.Components>
      <controls:TextComponent Text="{Binding EMP_LAST_NAME}"/>
      <controls:TextComponent Text="{Binding EMP_FIRST_NAME}"/>
    </controls:MultiComponentLabel.Components>
  </controls:MultiComponentLabel>
</ContentPage>

只需使用MultiComponentLabel替换多个Label 对于您的ListView
<ListView ItemsSource="{Binding EmployeesList}"
          HasUnevenRows="True">
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <Grid Padding="10" RowSpacing="10" ColumnSpacing="5">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

    <controls:CircleImage Source="icon.png"
           HeightRequest="66"
           HorizontalOptions="CenterAndExpand"
           Aspect="AspectFill"
           WidthRequest="66"
           Grid.RowSpan="2" />

    <controls:MultiComponentLabel Grid.Row="1" Grid.Column="1">
      <controls:MultiComponentLabel.Components>
        <controls:TextComponent Text="{Binding EMP_LAST_NAME}"/>
        <controls:TextComponent Text="{Binding EMP_FIRST_NAME}"/>
      </controls:MultiComponentLabel.Components>
    </controls:MultiComponentLabel>

  </Grid>
</ViewCell>


我该如何在XAML中实现这个? - Jaycee Evangelista
这适用于ListView吗? - Jaycee Evangelista
@JayceeEvangelista 在你的 ListView 中添加了我使用的代码。 - Yehor Hromadskyi

4

Xamarin Forms 4.7添加了多绑定的支持,可以像这样使用:

<Label>
    <Label.Text>
        <MultiBinding StringFormat="{}{0} {1}">
            <Binding Path="FirstName" />
            <Binding Path="LastName" />
        </MultiBinding>
    </Label.Text>
</Label>

在提供的示例中,“FirstName”和“LastName”是在类中定义的常规视图模型属性:
public string FirstName { get; set; }
public string LastName { get; set; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接