0

我正在制作一个带有 SOAP 服务的 Windows Phone 应用程序。我想将 XML 响应解析为 ListBox,但出现多个错误。

这是我所有的代码和日志:

Verkopen.xaml

(...)    
<ListBox Height="601" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="460" DataContext="{Binding}" Foreground="Black" FontSize="32" Grid.Row="0" Grid.RowSpan="1">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Sample" ></TextBlock>
                                <TextBlock Text="{Binding omschr}"></TextBlock>
                                <TextBlock Text="{Binding totalen}" ></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
(...)

Verkopen.xaml.cs

    (...)
     class XMLInfo
        {
            public string omschr { get; set; }
            public string totalen { get; set; }
        }
    (...)
private void ophalencompleet(object sender, SalesService.retSalesCompletedEventArgs e)
        {
     string XMLResponse = e.Result;
                var XResult = XElement.Parse(XMLResponse);

                var result = XResult.Descendants("Table").Select(t => new XMLInfo
        {
            omschr = t.Descendants("omschr").First().Value,
            totalen = t.Descendants("exclusief").First().Value,
        });

                foreach (var res in result)
                {
                    string omschr = res.omschr; // here i am able get the response
                    Debug.WriteLine("Wat in de tabel zou moeten: " + omschr);
                    string totalen = res.totalen;
                }

                listBox1.ItemsSource = result;
    (...)

SOAP 响应

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <expnr>672 </expnr>
    <artnr>GX-05           </artnr>
    <omschr>Glaswerk OP=OP                </omschr>
    <datum>2013-09-30T00:00:00+02:00</datum>
    <aantal>1</aantal>
    <exclusief>2.44</exclusief>
    <inclusief>2.95</inclusief>
    <loca_id>0490</loca_id>
  </Table>
  <Table>
    <expnr>672 </expnr>
    <artnr>LZ03-0008       </artnr>
    <omschr>Pot RH 50x15x24,5 zwart       </omschr>
    <datum>2013-09-30T00:00:00+02:00</datum>
    <aantal>1</aantal>
    <exclusief>20.62</exclusief>
    <inclusief>24.95</inclusief>
    <loca_id>0490</loca_id>
  </Table>
</NewDataSet>

错误:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
System.Windows.Data Error: Cannot get 'omschr' value (type 'System.String') from 'CtrlBase.XMLInfo' (type 'CtrlBase.XMLInfo'). BindingExpression: Path='omschr' DataItem='CtrlBase.XMLInfo' (HashCode=241995340); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: CtrlBase.XMLInfo.get_omschr()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[] inA first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
System.Windows.Data Error: Cannot get 'totalen' value (type 'System.String') from 'CtrlBase.XMLInfo' (type 'CtrlBase.XMLInfo'). BindingExpression: Path='totalen' DataItem='CtrlBase.XMLInfo' (HashCode=241995340); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: CtrlBase.XMLInfo.get_totalen()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[]A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
System.Windows.Data Error: Cannot get 'omschr' value (type 'System.String') from 'CtrlBase.XMLInfo' (type 'CtrlBase.XMLInfo'). BindingExpression: Path='omschr' DataItem='CtrlBase.XMLInfo' (HashCode=241995360); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: CtrlBase.XMLInfo.get_omschr()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[] inA first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
System.Windows.Data Error: Cannot get 'totalen' value (type 'System.String') from 'CtrlBase.XMLInfo' (type 'CtrlBase.XMLInfo'). BindingExpression: Path='totalen' DataItem='CtrlBase.XMLInfo' (HashCode=241995360); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: CtrlBase.XMLInfo.get_totalen()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[]A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
System.Windows.Data Error: Cannot get 'omschr' value (type 'System.String') from 'CtrlBase.XMLInfo' (type 'CtrlBase.XMLInfo'). BindingExpression: Path='omschr' DataItem='CtrlBase.XMLInfo' (HashCode=241995377); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: CtrlBase.XMLInfo.get_omschr()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[] inA first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll
System.Windows.Data Error: Cannot get 'totalen' value (type 'System.String') from 'CtrlBase.XMLInfo' (type 'CtrlBase.XMLInfo'). BindingExpression: Path='totalen' DataItem='CtrlBase.XMLInfo' (HashCode=241995377); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: CtrlBase.XMLInfo.get_totalen()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[]The program '[267518466] UI Task: Managed' has exited with code 0 (0x0).

谁能帮我解决这个问题。我在谷歌上搜索了很多,但没有找到解决方法。非常感谢!

4

1 回答 1

0

我在一篇博文中写过这个问题,在这里查看

问题在于 Windows Phone 7 上的反射 API。有趣的是,如果 WP7 应用程序在 Windows Phone 8 设备或模拟器上运行,则上面的代码将起作用,因为在 WP8 上,您可以访问私有/内部的东西。

如果您遇到数据绑定问题,请检查类和/或属性是否是公共的。并尝试在启用了 break-on-thrown-exceptions 的调试模式下运行代码。

于 2013-10-01T11:55:36.003 回答