【问题标题】:How to return ArrayList type from DomainService Class to CLient Side?如何将 ArrayList 类型从 DomainService 类返回到客户端?
【发布时间】:2009-10-23 15:42:03
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    我很抱歉没有发布我找到的解决方案。老板给我的工作比我能应付的多。 :) 请注意,我的解决方案可能不是最好的,但由于我对 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
    

    【讨论】:

    • 感谢 Logansama 的解决方案 :D 我搜索了整个互联网,谢谢 :D 你太棒了!!!
    【解决方案2】:

    不确定是否可以返回 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>
    

    【讨论】:

    • 谢谢。我设法返回了一个似乎有效的 List 项目。但现在我正在寻找如何让这些数据显示在组合框中的新航程,或者正如他们所说的将数据绑定到组合框。
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 2014-04-23
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多