2012-04-13 6 views
1

ich ein Windows Phone 7 App schreibe, und ich habe eine XML-Datei, die wie dies MirIch kann die Suchergebnisse von XElement-Abfrage in listbox bekommen, aber nicht von listbox zu XAML-Datei

<Barcodes> 
    <Barcode code="7393997155338" 
      companyName="Garbo Food AB" 
      productName="Toast Loaf"/> 

</Barcodes> 

sieht die XML bin auf der Suche Datei mit xElement wie unten, bekomme ich nicht null auf der Listbox, die mir ihre bevölkerten?!?

 var searchElement = XElement.Load("storedBarcodes.xml"); 
     var barcodeProductResult = from var in searchElement.Descendants("Barcodes") 
            where var.Attribute("code").Value == b 
            select new ProductBarcodes() 
            { 
             code = var.Attribute("code").Value, 
             companyName = var.Attribute("companyName").Value, 
             productName = var.Attribute("productName").Value 

            }; 
     if (barcodeProductResult != null) 
     { 
      tbSuccessfullSearchOrNot.Text = ("Search has found a matching product!"); 

      if (resultsListBox != null) 
      { 

       resultsListBox.ItemsSource = barcodeProductResult; 
       tbSuccessfullSearchOrNot.Text = ("Search has found a matching product! but listbox is not empty"); 
      } 
      else 
      { 

       tbSuccessfullSearchOrNot.Text = ("Search has found a matching product! but listbox is empty"); 
      } 
       //tbProductBarcode.Text = ("Product Barcode : " + _code + Environment.NewLine); 
      //tbCompanyName.Text = ("Company Name: " + _companyName + Environment.NewLine); 
     } 
     else 
     { 
      tbSuccessfullSearchOrNot.Text = ("No matching product found!"); 
     } 

und nach dem listbox es sollte wie unten auf dem Bildschirm ausgegeben werden, ausgefüllt ist, ich ein paar Möglichkeiten, ich versuche also. mit {Binding Path = Element [code] .Wert} "und nur Text =" {Binding Code}, das Ergebnis auf Ausgang Bildschirm leer ist,

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <StackPanel> 
     <Button Height="150" Content="Press here to scan a barcode !!" Click="Button_Click" Foreground="#FF2FFF07" /> 
     <TextBlock Margin="30" FontSize="36" TextWrapping="Wrap" x:Name="tbSuccessfullSearchOrNot"/> 
     <TextBlock Margin="30" FontSize="36" TextWrapping="Wrap" x:Name="tbScanResultBarCode"/> 
     <ListBox x:Name="resultsListBox" TabIndex="8" ItemsSource="barcodeProductResult" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Margin="10" > 
         <TextBlock x:Name="results" Text="{Binding code}" FontSize="22" FontWeight="Bold" FontStyle="Italic" Foreground="Orange" /> 
         <TextBlock Text="{Binding Path=Element[code].Value}" Foreground="Black"/> 
         <TextBlock Text="{Binding companyName}" Foreground="Black"/> 
         <TextBlock Text="{Binding productName}" Foreground="Black"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <!--<TextBlock Height="346" FontSize="36" x:Name="tbProductDetails" TextWrapping="Wrap" Width="450" > 

     </TextBlock>--> 
     <TextBox Height="71" Name="tbProductBarcode" Width="460" IsEnabled="False" IsReadOnly="True" DataContext="{Binding ElementName=tbProductBarcode}" /> 
     <TextBox Height="71" Name="tbCompanyName" Width="460" IsEnabled="False" IsReadOnly="True" /> 
     <TextBox Height="71" Name="tbProductName" Width="460" IsEnabled="False" IsReadOnly="True" /> 
     <!--<Button Height="150" Content="Tap to scan a QR code" Click="Button_Click_1"/> 
     <TextBlock Margin="30" FontSize="36" TextWrapping="Wrap" x:Name="tbScanResultQR"/>--> 

    </StackPanel> 
</Grid> 

Beitrag Edit: Dies ist der Code im stützt meine Suche auf. Dies funktioniert,

 private void menuBtn_Click(object sender, RoutedEventArgs e) 
    { 
     var element = XElement.Load("SearchFile.xml"); 
     var filteredData = from var in element.Descendants("chipper") 
          where var.Attribute("County").Value.ToLower()==tbSearch.Text.ToLower() 
          select new Counties() 
          { 
           LName = var.Attribute("LName").Value, 
           PlAddress = var.Attribute("PlAddress").Value, 
           County= var.Attribute("County").Value, 
           PhoneNumber = var.Attribute("PhoneNumber").Value 


          }; 

     ChipperListBox.ItemsSource = filteredData; 
    } 

und XAML:

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,90" Orientation="Horizontal"> 
    <ListBox x:Name="ChipperListBox" SelectionChanged="listBox_SelectionChanged"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Margin="10" > 
        <TextBlock x:Name="chipName" Text="{Binding LName}" FontSize="22" FontWeight="Bold" FontStyle="Italic" Foreground="Orange" Tap="chipName_Tap" /> 
        <TextBlock Text="{Binding PlAddress}" Foreground="Black"/> 
        <TextBlock Text="{Binding County}" Foreground="Black"/> 
        <TextBlock Text="{Binding PhoneNumber}" Foreground="Black"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</StackPanel> 

EDIT: Ich un diese App auf einem Windows-Telefon, weil der Emulator leider kein camera.heres die Infos von Call-Stack hat, nachdem das Element Namen ändern zu „Barcode“ ich bekomme einen readerException, die den Barcode-Scanner fast sofort stoppt, aber wenn ich das Debuggen stoppen i laufen wieder außerhalb von Visual Studio nur am Telefon und die App läuft, aber noch keine Ausgabe an die Listbox in der xAML

" Eine erste Chance Ausnahme des Typs 'com.google.zxing.ReaderException' aufgetreten in Silverlight_ZXing_Core.dll

Zusätzliche Informationen: ReaderException“

Silverlight_ZXing_Core.dll!com.google.zxing.oned.UPCEANReader.findGuardPattern(com.google.zxing.common.BitArray row, int rowOffset, bool whiteFirst, int[] pattern) + 0xf1 bytes  
    Silverlight_ZXing_Core.dll!com.google.zxing.oned.UPCEANReader.findStartGuardPattern(com.google.zxing.common.BitArray row) + 0x8 bytes 
    Silverlight_ZXing_Core.dll!com.google.zxing.oned.UPCEANReader.decodeRow(int rowNumber, com.google.zxing.common.BitArray row, System.Collections.Generic.Dictionary<object,object> hints)  
    Silverlight_ZXing_Core.dll!com.google.zxing.oned.OneDReader.doDecode(com.google.zxing.BinaryBitmap image, System.Collections.Generic.Dictionary<object,object> hints) + 0x113 bytes 
    Silverlight_ZXing_Core.dll!com.google.zxing.oned.OneDReader.decode(com.google.zxing.BinaryBitmap image, System.Collections.Generic.Dictionary<object,object> hints) 
    Silverlight_ZXing_Core.dll!com.google.zxing.oned.OneDReader.decode(com.google.zxing.BinaryBitmap image) + 0x3 bytes 
> WP7.ScanBarCode.dll!WP7.ScanBarCode.BarCode.cam_AutoFocusCompleted(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e) Line 114 + 0x7 bytes C# 
    Microsoft.Phone.dll!Microsoft.Devices.Camera.SafeFireEvent<Microsoft.Devices.CameraOperationCompletedEventArgs>.AnonymousMethod__1f(object ignored) + 0x17 bytes  
    mscorlib.dll!System.Threading.ThreadPool.WorkItem.WaitCallback_Context(object state) + 0x18 bytes 
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x63 bytes  
    mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(object o) + 0x47 bytes 
    mscorlib.dll!System.Threading.Timer.ring() + 0x70 bytes 

Antwort

3

Barcodes ist Ihr Wurzelelement und Sie möchten eine Liste von Barcode Artikel, so dass diese Verwendung als from:

from barcode in searchElement.Descendants("Barcode") 

als Randbemerkung, Ihre null Kontrollen sind nicht korrekt. barcodeProductResult wird nicht Null sein, aber es könnte eine Count() von 0 haben. resultsListBox wird auch nie Null sein.

+0

"Verwenden Sie dies als Ihre von: von Element in searchElement.Descendants (" Barcode ")" <- sollte ich "Barcode" anstelle von "Barcodes" verwenden? –

+0

Das stimmt. Ich habe meine Antwort aktualisiert, um sie klarer zu machen. – mafue

+0

ty für Randnotiz Ich habe geändert, um zu sehen, wie viele Elemente in der Liste sind if (resultsListBox.Items.Count() == 0) –

Verwandte Themen