2017-09-05 41 views
2

我在綁定Xamarin Picker時遇到了一些困難,因此當我更改所選項目時,它會觸發我的表單中的刷新。我使用帶代碼隱藏功能的XAML。在XAML中綁定到Xamarin Picker SelectedItem不是重新加載表單

這是XAML。你可以看到Picker定義的。數據被很好地綁定,它從我的列表中加載Name屬性。

它上面有一個Id「Entry」字段,這是不工作的部分。我希望這樣做的是當選擇器被「挑選」時,從列表/選定列表項中顯示Id字段。

還有一些示例代碼,我在網上找到的作品 - 姓和名都是在輸入數據時更新他相應的標籤。所以它必須與選擇器和SelectedItem綁定或我試圖使用OnPropertyChanged的方式有關。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TechsportiseApp.MainUI.Races"> 
    <StackLayout Padding="0,20,0,0"> 
    <Label Text="Race ID" /> 
    <Entry Text="{Binding Id}" /> 
    <Picker ItemsSource="{Binding RaceList}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding RacesIndex, Mode=TwoWay}" /> 
    <Label Text="Data Binding 101 Demo" FontAttributes="Bold" HorizontalOptions="Center" /> 
    <Label Text="Forename:" /> 
    <Entry Text="{Binding Forename, Mode=TwoWay}" /> 
    <Label Text="Surname:" /> 
    <Entry Text="{Binding Surname, Mode=TwoWay}" /> 
    <Label Text="Your forename is:" /> 
    <Label Text="{Binding Forename}" /> 
    <Label Text="Your surname is:" /> 
    <Label Text="{Binding Surname}" /> 
    </StackLayout> 
</ContentPage> 

我使用的ViewModel如下所示。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Threading.Tasks; 
using Newtonsoft.Json; 
using RestSharp; 
using TechsportiseApp.API.Models; 
using Xamarin.Forms; 
using TechsportiseApp.API; 

namespace TechsportiseApp.MainUI.Models 
{ 
public class RacesViewModel : INotifyPropertyChanged 
{ 
    int _id; 
    public int Id 
    { 
     get 
     { 
      return _id; 
     } 
     set 
     { 
      if (_id != value) 
      { 
       _id = value; 
       OnPropertyChanged("Id"); 
      } 
     } 
    } 
    string _name; 
    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      if (_name != value) 
      { 
       _name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 
    } 

    public List<Race> RaceList 
    { 
     get 
     { 
      var racelist = RacesAPI.GetRaces(); 
      return racelist; 
     } 
    } 

    int _racesIndex; 
    public int RacesIndex 
    { 
     get 
     { 
      return _racesIndex; 
     } 
     set 
     { 
      if (_racesIndex != value) 
      { 
       _racesIndex = value; 

       // trigger some action to take such as updating other labels or fields 
       OnPropertyChanged("RacesIndex"); 
      } 
     } 
    } 

    string forename, surname; 

    public string Forename 
    { 
     get 
     { 
      return forename; 
     } 
     set 
     { 
      if (forename != value) 
      { 
       forename = value; 
       OnPropertyChanged("Forename"); 
      } 
     } 
    } 

    public string Surname 
    { 
     get 
     { 
      return surname; 
     } 
     set 
     { 
      if (surname != value) 
      { 
       surname = value; 
       OnPropertyChanged("Surname"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     var changed = PropertyChanged; 
     if (changed != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName));    
      } 
    }  
} 

在情況下,它是必需的,這裏是我使用的填充我的列表數據,這我是從一個API的JSON響應獲取的代碼。

using Newtonsoft.Json; 
using RestSharp; 
using System; 
using System.Collections.Generic; 
using System.Text; 
using TechsportiseApp.API.Models; 
using TechsportiseApp.MainUI; 
using Xamarin.Forms; 

namespace TechsportiseApp.API 
{ 
    public class RacesAPI 
    { 
     public static List<Race> GetRaces() 
     { 
      var raceList = new List<Race>(); 
      string APIServer = Application.Current.Properties["APIServer"].ToString(); 
      string Token = Application.Current.Properties["Token"].ToString(); 
      var client = new RestClient(APIServer); 
      var request = new RestRequest("api/race", Method.GET); 
      request.AddHeader("Content-type", "application/json"); 
      request.AddHeader("Authorization", "Bearer " + Token); 
      var response = client.Execute(request) as RestResponse; 
      raceList = JsonConvert.DeserializeObject<List<Race>>(response.Content); 
      return raceList; 
     } 
    } 
} 

任何想法?我一直試圖解決這個問題,現在幾天,似乎無法使它工作。

回答

3

從我所看到的,你沒有設置Id的值。可能你想把它放在RacesIndex的setter中。另外RacesIndex應該綁定到SelectedIndex,而不是SelectedItem。我假設Race類有一個Id屬性,它是一個int類型,所以請看下面的內容。如果你想綁定到selectedItem,那麼你綁定的屬性應該與列表中的屬性相同

int _racesIndex; 
public int RacesIndex 
{ 
    get 
    { 
     return _racesIndex; 
    } 
    set 
    { 
     if (_racesIndex != value) 
     { 
      _racesIndex = value; 

      // trigger some action to take such as updating other labels or fields 
      Id = racelist[value].RaceId; 
      OnPropertyChanged("RacesIndex"); 
     } 
    } 
} 
+0

非常好,破解它 - 謝謝! –