[英]how to have 2 data binding fields in one Xamarin forms label?
你好,我有一個應用程序,我正在 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);
}
}
}
任何幫助都會很棒!
提前致謝:)
*更新:隨着 Xamarin Forms 4.7 的發布,您現在可以使用多重綁定而不是創建 getter 屬性。 使用名字和姓氏示例,您將執行以下操作:
<StackLayout Padding="20,0,0,0" HorizontalOptions="CenterAndExpand" >
<Label x:Name="FirstName">
<Label.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</Label.Text>
</Label>
.........
</StackLayout>
*Pre-Xamarin Forms 4.7 在這種情況下我所做的是在模型上放置一個額外的屬性來組合這兩個屬性。
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 時可能從未更改過名字或姓氏屬性,因此您只需將該屬性添加到您的模型中,就像我在上面的ContactInfo
代碼中顯示的那樣,然后更改您的標簽,就像我顯示的那樣在上面的編輯中,你會很高興。
看起來像Label可以包含多個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>
這僅適用於XF 3.1版以上,而不是下面。
在早期版本中,Span.Text不可綁定。 上面的XAML沒有用; 它拋出異常。 所以這可能不適合你。
Afzal Ali評論說,截至2018年8月,這在XF 3.1中有效。
正如 Ed Phuket 所說,它正在運行,但是:
如果您希望它使用OnPropertyChanged
事件更新跨度,則需要添加Mode=OneWay
,因為它默認為OneTime
Binding 模式
雖然 Xamarin Forms 的團隊仍在研究使用 Span 元素進行綁定,但您只需更改標簽的格式即可使其正常工作:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.