简体   繁体   中英

ObservableCollection vs Normal List for Async Webservices Call

All,

I was just implementing a Webservices method that needs to take as an argument a list of objects. On the client side, I tried to call this passing it a list of the objects, but the compiler complained saying that it needed an ObservableCollection instead. Here is the code:

Server Side

[WebMethod]
public void SaveMarks(List<CompletedMark> marks)
{
    // TODO: IMPLEMENT SAVING THE MARKS
}

Client Side

private void SaveMarks()
{
    ObservableCollection<CompletedMark> marks = new ObservableCollection<CompletedMark>();
    //List<CompletedMark> marks = new List<CompletedMark>();

    foreach (SelectedField elem in SelectedFields)
    {
        marks.Add(new CompletedMark
                      {
                          FormId = curFormId,
                          QuestionId = elem.Qid,
                          XPos = Canvas.GetLeft(elem.assocGrid),
                          YPos = Canvas.GetTop(elem.assocGrid),
                          Width = elem.assocGrid.Width,
                          Height = elem.assocGrid.Height
                      });
    }
    proxy.SaveMarksAsync(marks);
}

I have commented out the line where I originally specified the List. Why is it that I need to use an ObservableCollection here even though I declared it as type List on the server? Is my server-side declaration incorrect?

The default type collections are serialized to in Silverlight is ObservableCollection, mainly for ease in using it for DataBinding purposes. Ehsan's answer is correct; you can change what the default type is in the Configure Service Reference dialog.

Alternatively, you can change this to serialize to a generic list (probably more useful if you have a variety of consumers, or if this is more of a data-transfer-object instead of something you'd bind directly to).

Go to your client project -> Service References -> [Your Proxy] and then Right-Click and select Configure Service Reference. in the new opened dialog you can set the way you like to have collections serialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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