繁体   English   中英

将json数据加载到wpf datagrid

[英]load json data to wpf datagrid

我想将json数据加载到我的wpf datagrid中。 我已遵循本教程: http : //wpf-4-0.blogspot.com/2012/12/how-to-bind-json-data-to-datagrid-in.html

但是出了点问题,我不确定问题出在哪里。


我的XAML程式码:

<Button Click="btnLoad_Click" Content="Load" Width="50" Grid.Row="0"></Button>
<DataGrid Grid.Row="1" x:Name="albuminfo" AutoGenerateColumns="True"></DataGrid>

C#代码:

    private void btnLoad_Click(object sender, RoutedEventArgs e)
    {
        WebRequest req = WebRequest.Create("http://coderomusic.com/beta/SecuredAdministrator/mobile/getInfo.php?_mod=album");
        req.ContentType = "application/json";
        WebResponse resp = req.GetResponse();
        Stream stream = resp.GetResponseStream();
        StreamReader re = new StreamReader(stream);
        string json = re.ReadToEnd();
        json = "{\"AlbumDetail\":" + json + "}";

        Wrapper w = (Wrapper)new JavaScriptSerializer().Deserialize(json, typeof(Wrapper));

        albuminfo.ItemsSource = w.albumdetail;
    }

AlbumDetail类:

public class AlbumDetail
{
    #region//private property

    private int albumId;
    private string albumName;
    private DateTime releasedate;
    private int productionId;
    private string duration;
    private string category;
    private string themes;
    private ImageBrush coverImage;
    private ImageBrush profileImage;
    private string description;
    private double albumPrice;
    private double specialPrice;
    private int[] artistIds;

    #endregion


    #region//public property

    public int[] ArtistIds
    {
        get { return artistIds; }
        set { artistIds = value; }
    }


    public double SpecialPrice
    {
        get { return specialPrice; }
        set { specialPrice = value; }
    }


    public double AlbumPrice
    {
        get { return albumPrice; }
        set { albumPrice = value; }
    }

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    public ImageBrush ProfileImage
    {
        get { return profileImage; }
        set { profileImage = value; }
    }

    public ImageBrush CoverImage
    {
        get { return coverImage; }
        set { coverImage = value; }
    }

    public string Themes
    {
        get { return themes; }
        set { themes = value; }
    }

    public string Category
    {
        get { return category; }
        set { category = value; }
    }

    public string Duration
    {
        get { return duration; }
        set { duration = value; }
    }

    public int ProductionId
    {
        get { return productionId; }
        set { productionId = value; }
    }

    public DateTime Releasedate
    {
        get { return releasedate; }
        set { releasedate = value; }
    }

    public string AlbumName
    {
        get { return albumName; }
        set { albumName = value; }
    }

    public int AlbumId
    {
        get { return albumId; }
        set { albumId = value; }
    }

    #endregion
}

和包装类:

public class Wrapper
{
    public List<AlbumDetail> albumdetail { get; set; }
}

最终结果如下图所示: https : //dl.dropboxusercontent.com/u/59201118/json.JPG (Dropbox链接,因为不允许发布图像!)

您应该在后台线程上发出Web请求,然后使用调度程序对象更新UI。 我已经修改了您的代码以使其正常工作。 这是重要的部分,

    private static readonly ManualResetEvent AllDone = new ManualResetEvent(false);
    private void BtnLoadClick(object sender, RoutedEventArgs e)
    {
        var request =
            (HttpWebRequest)
            WebRequest.Create("http://coderomusic.com/beta/SecuredAdministrator/mobile/getInfo.php?_mod=album");
        request.ContentType = "application/json";
        request.BeginGetResponse(GetResponseCallback, request);
        AllDone.WaitOne();
    }

    private void GetResponseCallback(IAsyncResult ar)
    {
        var request = (HttpWebRequest) ar.AsyncState;
        var response = (HttpWebResponse) request.EndGetResponse(ar);
        Stream stream = response.GetResponseStream();
        Wrapper w = null;
        if (stream != null)
        {
            var re = new StreamReader(stream);
            string json = re.ReadToEnd();
            w = (Wrapper) new JavaScriptSerializer().Deserialize(json, typeof (Wrapper));
        }

        Dispatcher.BeginInvoke(
            new Action(() => {
                                 if (w != null)
                                     albuminfo.ItemsSource = new ObservableCollection<AlbumDetail>(w.AlbumDetail);
            }));
        response.Close();
        AllDone.Set();
    }

根据您的需要进行更改。 我希望它能起作用。

简单易用的方法是使用nuget中的jsonorm并通过( http://json2csharp.com/ )从接收到的json创建数据层

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM