implementing iobservable on dictionary object and

2019-08-31 03:56发布

i have this JSON which i am doing GET from webservice in my code and parsing it into a dictionary.

 {
        "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"]));

    }

I am curious as to how i can implement iobservable and inotifypropertychanged on the dict object and bind values to UI elements in XAML like each tile in XAML will have name,P1 and P2 . Any suggestions plz?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-31 04:42

First write a class for your objects and implement INotifyPropertyChanged.

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

Now your collection is not a Dictionary , it is ObservableCollection . In your getJSON_click Method you load the data into new objects of type YourNewClass.

查看更多
登录 后发表回答