您好我有一个应用程序我正在使用xamrian froms从服务中获取数据。我要做的是让名字和姓氏字段显示在同一标签中,但它当前显示名字然后它返回一行,然后显示姓氏。继承我的xaml代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ReadyMo.ContactInfo">
<ListView ItemsSource="{Binding .}" HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Padding="0,0,0,8" BackgroundColor="#d2d5d7">
<Frame.Content>
<Frame Padding="25,25,25,25" OutlineColor="Gray" BackgroundColor="White">
<Frame.Content>
<StackLayout Padding="20,0,0,0" HorizontalOptions="CenterAndExpand" >
<Label x:Name="FirstName" Text="{Binding First_Name}">
</Label>
<Label x:Name ="LastName" Text="{Binding Last_Name}">
</Label>
<Label x:Name="County" Text="{Binding name}">
</Label>
<Label x:Name ="Adress" Text="{Binding Address1}">
</Label>
<Label x:Name ="City" Text="{Binding Address2}">
</Label>
<Label x:Name="Number" Text="{Binding BusinessPhone}" >
</Label>
</StackLayout>
</Frame.Content>
</Frame>
</Frame.Content>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
编辑这是我的代码隐藏:
using Newtonsoft.Json;
using ReadyMo.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ReadyMo
{
public partial class ContactInfo : ContentPage
{
private County item;
public static async Task<string> GetContactString(string contactid)
{
HttpClient client = new HttpClient();
var url = $"URL";
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var responsetext = await response.Content.ReadAsStringAsync();
return responsetext;
}
throw new Exception(response.ReasonPhrase);
}
public ContactInfo()
{
InitializeComponent();
ContactInfoList = new ObservableCollection<ContactInfoModel>();
}
ObservableCollection<ContactInfoModel> ContactInfoList;
public ContactInfo(County item) : this()
{
this.item = item;
this.BindingContext = ContactInfoList;
}
protected override async void OnAppearing()
{
if (item == null)
return;
var contact = await GetContactString(item.id);
var models = JsonConvert.DeserializeObject<List<ContactInfoModel>>(contact);
foreach (var model in models)
ContactInfoList.Add(model);
}
}
}
任何帮助都会令人惊叹!
提前致谢:)
答案 0 :(得分:15)
在这种情况下我做的是在组合两个属性的模型上添加一个额外的属性。
public class ContactInfo {
public string FirstName { get; set; }
public string LastName { get; set; }
public string FirstLastName { get { return FirstName + " " + LastName; }}
//Or use C# 6 goodness
//public string FirstLastName => FirstName + " " + LastName;
}
现在在您的ViewModel中,如果名字或姓氏发生变化,您需要执行以下操作来更新FirstLastName
属性:
private string _firstLastName;
public string FirstLastName {
get { return _firstLastName; }
set {
if(_firstLastName != value) {
_firstLastName = value;
SetPropertyChanged();
}
}
}
private string _firstName;
public string FirstName {
get { return _firstName; }
set {
if(_firstName != value) {
_firstName = value;
SetPropertyChanged();
SetPropertyChanged("FirstLastName"); //Also send alert that FirstLastName changed
}
}
}
然后为您LastName
属性执行相同的操作。
编辑:您的XAML将如下所示:
<StackLayout Padding="20,0,0,0" HorizontalOptions="CenterAndExpand" >
<Label x:Name="FirstName" Text="{Binding FirstLastName}"/>
.....
</StackLayout>
Edit2:因为您在显示UI时可能永远不会更改First或Last Name属性,您只需要将属性添加到模型中,就像我在上面的ContactInfo
代码中显示的那样,然后改变你的标签,就像我在上面的编辑中所显示的那样,你会很高兴。
答案 1 :(得分:12)
看起来像a Label can contain multiple Spans,与WPF中的TextBlock / Run大致相似:
<Label FontSize=20>
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding FirstName}" ForegroundColor="Red" FontAttributes="Bold" />
<Span Text="{Binding MI}" />
<Span Text="{Binding LastName}" FontAttributes="Italic" FontSize="Small" />
</FormattedString>
</Label.FormattedText>
</Label>
结果是Span.Text不可绑定。上面的XAML不起作用;它抛出异常。但我会把它留在这里,所以没有其他人浪费时间在我所做的同样注定的概念上。
Afzal Ali评论说,自2018年8月起,这在XF 3.1中有效。
答案 2 :(得分:-1)
虽然Xamarin Forms的团队仍在研究如何使用Span元素进行绑定,但是您只需更改Labels的格式即可使用此功能:
<StackLayout Orientation="Horizontal" Padding="0" Margin="0" Spacing="0">
<Label Text="{Binding First_Name}" Margin="0" />
<Label Text=" " Margin="0" />
<Label Text="{Binding Last_Name}" Margin="0" />
</StackLayout>