如何将ArrayList类型从DomainService类返回到CLient Side?

时间:2009-10-23 15:42:03

标签: silverlight-3.0 arraylist wcf-ria-services

背景:使用RIA服务理念,在Silverlight3 / C#/ .Net中有3-4周的经验,价值约为3天。 (我以前的大部分问题都应该解释原因)

我正在使用Silverlight3测试Microsoft的RIA服务。这是我必须为客户做的概念验证的一部分。所以它非常基础。 我已经弄清楚如何使用RIA服务等构建Silverlight3项目。因此,传递和返回字符串和int是没有问题的。

但是我需要从我的域服务类返回一个ArrayList到我的SL3客户端。但它似乎原样传回一个ArrayList,是不允许的。而我对C#的有限知识无助于快速进行类型转换/转换等。这个服务器端函数得到一个必须返回给SL3客户端的ArrayList,所以我必须用它做一些事情来发送它到客户端。

问题: 有谁知道应该对ArrayList(在c#中)做什么来允许DomainService类函数将它返回给调用客户端/ SL3函数?

[注意:>我的大多数尝试都以错误结束:“名为'myFunctionName'的服务操作不符合所需的签名。返回和参数类型必须是实体类型或一种预定义的可序列化类型。“]

请随时索取您认为合适的任何信息。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

我很抱歉没有找到我发现的解决方案。老板们给我的工作比我能处理的要多。 :) 请注意我的解决方案可能不是最好的,但由于我对SL和RIA服务的了解是如此新,我想这可能是可以原谅的。最初我想从客户端提供的代码中传回相当复杂的数组,但努力和时间限制使我只能正确转换并返回List。 希望这在某种程度上有所帮助。

客户端:MainPage.xaml.cs中的Silverlight代码我调用从服务器端检索数据列表,以显示在dropDown列表中。

// Function called on load of the SL interface
// 'slayer' is an object of the Domain Service Class server-side
// 'this.gidSessionNumber' is just a number used in the demo to represent a session
public void loadPaymentTypeComboBox()
{
    InvokeOperation<IEnumerable<string>> comboList = sLayer.getPaymentTypeCombo(this.gidSessionNumber);
    comboList.Completed += new EventHandler(popPaymentCombo_complete);
}//function loadAllComboBoxes

// Event handler assigned
public void popPaymentCombo_complete(object sender, EventArgs e)
{
    InvokeOperation<IEnumerable<string>> obj = (InvokeOperation<IEnumerable<string>>)sender;
    string[] list = obj.Value.ToArray();

    // 'paymentTypeDropdown' is the name of the specific comboBox in the xaml file
    paymentTypeDropdown.IsEnabled = true;

    // Assign the returned arrayList as itemSource to the comboBox
    paymentTypeDropdown.ItemsSource = list;
}

在域服务类中,我有相关的功能:

    [ServiceOperation]
    public List<string> getPaymentTypeCombo(string gidNumber)
    {
        // Build objects from libraries provided by our client
        SDT.Life.LifeCO.clsSystemCreator.CreateSysObjects(gidNumber);
        this.lobjSys = SDT.Life.LifeCO.clsSystemCreator.GetSysObject(gidNumber);

        // Rtrieve the ArrayList from the client's code       
        clsTextList comboList= this.lobjSys.lstPaymentType_PaymentQueue;

        // Get the length of the returned list
        int cnt= (int)comboList.Count();

        // Create the List<string> which will be populated and returned
        List<string> theList= new List<string>();

        // Copy each element from the clsTextList to the List<string>
        for (int i = 0; i < cnt;i++)
        {
            string status= comboList.Item(i).Description;
            theList.Add(status);
        }

        // return the newly populated List<string>
        return theList;
    }//end function getPaymentTypeCombo

答案 1 :(得分:1)

不确定是否可以返回ArrayList。我想你应该考虑返回一个IEnumerable,这将使服务将该方法识别为Read方法。

如果你有一个List或ObservableCollection并希望将它绑定到像ComboBox这样的ItemControl,你可以在ItemControl上设置ItemsSource。使用ItemControl上的DisplayPath属性来设置要显示的属性或使用DataTemplate。

<ComboBox>
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <TextBlock Text={"Binding Path=Property1"}/>
        <TextBlock Text={"Binding Path=Property2"}/>
        <TextBlock Text={"Binding Path=Property3"}/>
      </StackPanel>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>
相关问题