开发者

Implementing methods in WS-stub-classes

I am developing an Windows phone7 application, I have a .NET webservice that returns a List. This list should be bound to a ListBox.

Product is a class defined in the server and Visual Studio gets its definition from WSDL and creates a stub in my windows phone application.

What happens is that the list calls the "toString()" method in order to show the items in the GUI, so I have a strange string on my list. I would like to change to a user friendly string (Name - Quantity). To do that, I want to override the toString() method, but chang开发者_运维问答ing it in the server definition doesn't help because methods are not exposed in the WSDL. Changing the stub myself would cause me to lose the data when I refresh or change the reference.

I think it is a very often case, so there should be a solution, which I am missing. So please, how can I define the method ToString() in the windows phone7 to this object so I can bind it to the list?

Thank you, Oscar


The correct approach is to take the raw data coming in from the WS and translate it to a representative model, in adherence to the MVVM guidelines and seperating yourself from concerns on the server side. Creating a proxy essentially between you and the WS.

Furthermore, once you have done this you could simply create a property on that model which you can then bind to and could return the data as you need, making use of the DisplayMemberPath property.

<ListBox ItemsSource="{Binding}"
         DisplayMemberPath="Title">

Your model could then be something like...

public String Title
{
    get
    {
         return Name + Quantity;
    }
}


Are the stubs generated as partial classes which don't override ToString themselves? If so, it's easy - add your own partial class. For example:

// Autogenerated stub provides the rest of the code
public partial class Order
{
    public override string ToString()
    {
        return string.Format("{0} - {1}", Name, Quantity);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜