2009-10-23 20 views
1

背景:在Silverlight3/C#/ .Net和3-4天左右的RIA Services概念价值。 (我以前的大多数问题都应该解释为什么)如何将ArrayList类型从DomainService类返回到CLient端?

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

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

问: 有谁知道什么应该做一个ArrayList(在C#)允许的DomainService类函数将其返回到调用客户端/ SL3功能?

[注:我的大多数尝试都最终在错误的:“命名的服务操作‘myFunctionName’不符合所需的签名都返回和参数类型必须为实体类型或预定义之一。可序列化的类型。“]

请随时要求任何您认为合适的信息。 预先感谢您。

回答

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 

+0

感谢Logansama的解决方案:D我搜索了整个互联网,谢谢你:D你真棒! – 2011-02-15 20:51:38

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> 
+0

谢谢。我设法返回一个列表项目,这似乎工作。但是现在我正在进行一次新的旅程,以查找如何让这些数据显示在组合框中,或者正如他们所说将数据绑定到组合框。 – ddtpoison777 2009-10-30 14:10:34