Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以XAML不重新加载形式绑定到Xamarin选择器SelectedItem_C#_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

C# 以XAML不重新加载形式绑定到Xamarin选择器SelectedItem

C# 以XAML不重新加载形式绑定到Xamarin选择器SelectedItem,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我在绑定我的Xamarin选择器时遇到了一些困难,因此当我更改所选项目时,它会触发表单中的刷新。我正在使用XAML和代码隐藏 这是XAML。您可以看到已定义的选择器。数据正被很好地绑定到这一点,它正在从我的列表中加载Name属性 上面有一个Id“Entry”字段,这是不工作的部分。我希望这样做的是,当选择器被“拾取”时,显示列表/所选列表项中的Id字段 还有一些我在网上找到的示例代码,在输入数据时,姓氏和名字都在更新相应的标签。因此,这一定与选择器和SelectedItem绑定有关,或者与我尝试

我在绑定我的Xamarin选择器时遇到了一些困难,因此当我更改所选项目时,它会触发表单中的刷新。我正在使用XAML和代码隐藏

这是XAML。您可以看到已定义的选择器。数据正被很好地绑定到这一点,它正在从我的列表中加载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));               
            }
    }       
 }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Threading.Tasks;
使用Newtonsoft.Json;
使用RestSharp;
使用TechsportiseApp.API.Models;
使用Xamarin.Forms;
使用TechsportiseApp.API;
命名空间TechsportiseApp.MainUI.Models
{
公共类RacesViewModel:INotifyPropertyChanged
{
内部id;
公共整数Id
{
得到
{
返回_id;
}
设置
{
如果(_id!=值)
{
_id=值;
OnPropertyChanged(“Id”);
}
}
}
字符串\u名称;
公共字符串名
{
得到
{
返回_name;
}
设置
{
如果(_name!=值)
{
_名称=值;
不动产变更(“名称”);
}
}
}
公开名单参赛者
{
得到
{
var racelist=RacesAPI.GetRaces();
返回赛马名单;
}
}
国际赛车指数;
公共国际赛车索引
{
得到
{
返回_racesIndex;
}
设置
{
如果(_racesIndex!=值)
{
_消旋指数=数值;
//触发一些要执行的操作,例如更新其他标签或字段
OnPropertyChanged(“RacesIndex”);
}
}
}
字符串名称、姓氏;
公共字符串名
{
得到
{
返回名字;
}
设置
{
如果(名字!=值)
{
名字=价值;
OnPropertyChanged(“名字”);
}
}
}
公共字符串姓氏
{
得到
{
返回姓氏;
}
设置
{
如果(姓氏!=值)
{
姓氏=价值;
关于财产变更(“姓氏”);
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
var changed=Property changed;
如果(已更改!=null)
{
PropertyChanged(这是新的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;
        }
    }
}
使用Newtonsoft.Json;
使用RestSharp;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用TechsportiseApp.API.Models;
使用TechsportiseApp.MainUI;
使用Xamarin.Forms;
命名空间TechsportiseApp.API
{
公营赛马
{
公共静态列表GetRaces()
{
var raceList=新列表();
字符串APIServer=Application.Current.Properties[“APIServer”].ToString();
字符串标记=Application.Current.Properties[“Token”].ToString();
var client=新的RestClient(APIServer);
var请求=新的重新请求(“api/race”,Method.GET);
AddHeader(“内容类型”、“应用程序/json”);
请求.AddHeader(“授权”、“承载人”+令牌);
var response=client.Execute(request)as response;
raceList=JsonConvert.DeserializeObject(response.Content);
返回赛马名单;
}
}
}

有什么想法吗?我已经尝试破解这个问题好几天了,但似乎无法让它正常工作。

从我所看到的,你在任何时候都不能设置Id的值。可能你想把它放到RacesIndex的setter中。RacesIndex也应该绑定到SelectedIndex,而不是SelectedItem。我假设Race类有一个Id property是一个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");
        }
    }
}