1

背景: 3-4 周的 Silverlight3/C#/.Net 经验和大约 3 天的 RIA 服务概念经验。(我之前的大部分问题都应该解释为什么)

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

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

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

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

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

4

2 回答 2

2

我很抱歉没有发布我找到的解决方案。老板给我的工作比我能应付的多。:) 请注意我的解决方案可能不是最好的,但由于我在 SL 和 RIA 服务方面的知识太新了,我想这可能会被原谅。最初我想从我们的客户提供的代码中传回相当复杂的数组,但努力和时间的限制让我只能正确地转换并返回一个列表。希望这在某种程度上有所帮助。

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

// 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

于 2009-11-26T12:13:20.330 回答
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>
于 2009-10-26T18:30:29.890 回答