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

Jay*_*sta 9 xaml xamarin xamarin.forms

我希望我的两个字符串只能在一行上显示.是否有可能像这样:

库里斯蒂芬

使用此代码

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>
Run Code Online (Sandbox Code Playgroud)

jze*_*ino 14

您无法绑定到多个属性View Element.

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

例:

public class EmployeeViewModel
{
    public string FirstName { get; set; }    
    public string LastName { get; set; }    
    public string FullName => $"{FirstName} {LastName}";
}
Run Code Online (Sandbox Code Playgroud)

然后在XAML:

<Label Text="{Binding FullName}"/>
Run Code Online (Sandbox Code Playgroud)


Sha*_*tar 6

 <Label Text="{Binding Value, StringFormat='string before value {0:F0} string after value'}"/>
Run Code Online (Sandbox Code Playgroud)

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

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


Moh*_*ati 5

使用FormattedTextxamarin 形式的属性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>
Run Code Online (Sandbox Code Playgroud)

Xamarin.Forms 标签

您还可以添加Style以避免代码中的TextColor,FontSize和其他属性的代码重复。

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