字典对象上执行的IObservable和分配密钥值XAML元素(implementing iobse

2019-10-18 03:07发布

我有这个JSON对此我从web服务在我的代码执行GET,并解析成一个字典。

 {
        "X-YZ-111/AB.CD": {
            "P1": "F",
            "P2": "43.46"
        },        

        "X-YZ-112/AB.CD": {
            "P1": "F",
            "P2": "8.02"
        },
        "X-YZ-113/AB.CD": {
            "P1": "F",
            "P2": "9066.58"
        },
        "X-YZ-114/AB.CD": {
            "P1": "F",
            "P2": "6.00"
        },
        "X-YZ-115/AB.CD": {
            "P1": "F",
            "P2": "6.00"
        },        
        "X-YZ-116/AB.CD": {
            "P1": "F",
            "P2": "10.00"
        }}


    Using Windows.Data.Json;

    private async void getJSON_click(object sender,RoutedEventArgs e)

    { 
       var client=new HttpClient();
       client.MaxResponseBufferSize=1024*1024;
       var response= await Client.GetAsync(new Uri(The URL here));
       var result = await response.Content.ReadAsStringAsync();

       var jObj = JObject.Parse(result);
    var dict = jObj.Children()
               .Cast<JProperty>()
               .ToDictionary(p => p.Name, 
                             p => new Tuple<string, string>((string)p.Value["P1"], (string)p.Value["P2"]));

    }

我很好奇,我怎么能实现的IObservable和INotifyPropertyChanged接口的字典对象,并绑定值在XAML UI元素就像在XAML每个区块都会有名字,P1和P2。 任何建议PLZ?

Answer 1:

首先写你的对象类,并执行INotifyPropertyChanged。

public class YourNewClass : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }
    // Same for other two properties
}

现在您的收藏不是字典,它的ObservableCollection。 在你getJSON_click方法加载数据到类型YourNewClass的新对象。



文章来源: implementing iobservable on dictionary object and assigning key values to XAML elements