如何在一个Xamarin表单标签中具有2个数据绑定字段? [英] how to have 2 data binding fields in one Xamarin forms label?

查看:84
本文介绍了如何在一个Xamarin表单标签中具有2个数据绑定字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个正在使用xamrian froms开发的应用程序,该应用程序可从服务获取数据.我想做的是使名字和姓氏字段显示在同一标签中,但是当前显示名字,然后返回一行,然后显示姓氏.这是我的xaml代码:

Hello I have a app i'm working on in xamrian froms that gets data from a service. what i'm trying to do is make the first name and last name fields display in the same label however it currently displays first name then it returns a line and then shows the last name. heres my xaml code:

  <?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>

编辑这是我的代码背后:

EDIT Here's My codebehind:

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);



        }

    }
}

任何帮助都将是惊人的!

any help would be amazing!

先谢谢您了:)

推荐答案

*更新:随着Xamarin Forms 4.7的发布,您现在可以使用Multi-Bindings而不是创建getter属性.使用名字和姓氏示例,您将执行以下操作:

*Update: With the release of Xamarin Forms 4.7, you can now use Multi-Bindings instead of creating a getter property. Using the first and last name example, you would do something like this:

<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>
    </Labe>
    .....
</StackLayout>

* Xamarin之前的表格4.7 在这种情况下,我要做的是在结合了这两个属性的模型上放置一个额外的属性.

*Pre-Xamarin Forms 4.7 What I do in this situation is to put an extra property on the model that combines the two properties.

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属性:

Now in your ViewModel, if first or last name changes, you would need to do something like this to update the FirstLastName property:

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属性执行相同的操作.

Then do the same for you LastName property.

您的XAML如下所示:

Your XAML would then look like:

<StackLayout Padding="20,0,0,0"  HorizontalOptions="CenterAndExpand" >
    <Label x:Name="FirstName" Text="{Binding FirstLastName}"/>
    .....
</StackLayout>

Edit2:因此,由于您可能在显示UI时从未更改过First(姓)或Last Name(姓氏)属性,因此只需将属性添加到模型中(如我在上面的ContactInfo代码中所示),然后更改您的标签,就像我在上面的编辑中显示的那样,您会很方便.

So since you are probably not ever changing the First or Last Name property while showing the UI, you just need to add the property to your model, like I show in the ContactInfo code above, and then change your label, like I show in the edit above and you will be good to go.

这篇关于如何在一个Xamarin表单标签中具有2个数据绑定字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆