2013-09-02 4 views
0

XML 파일 데이터에서 LongListSelector를 만들려고합니다.WP8 XML 및 LongListSelector 작업

public MainPage() 
    { 
     InitializeComponent(); 

     XDocument loadedData = XDocument.Load("/Resources/EuroTrip.xml"); 

     var data = from query in loadedData.Descendants("country") 
        select new Country 
        { 
         Name = (string)query.Element("name"), 
         IdentityCard = (string)query.Element("identityCard"), 
         CompulsoryDocuments = (string)query.Element("compulsoryDocuments"), 
         Regulations = (string)query.Element("regulations"), 
         Equipment = (string)query.Element("equipment"), 
         SpeedLimitsLightVehicles = (string)query.Element("speedLimitsLightVehicles"), 
         AutoClubs = (string)query.Element("autoClubs") 
        }; 
     countriesList.ItemsSource = data; 
     // Set the data context of the listbox control to the sample data 
     DataContext = App.ViewModel; 
    } 

    public class Country 
{ 
    string name; 
    string identityCard; 
    string compulsoryDocuments; 
    string regulations; 
    string equipment; 
    string speedLimitsLightVehicles; 
    string autoClubs; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    public string IdentityCard 
    { 
     get { return identityCard; } 
     set { identityCard = value; } 
    } 

    public string CompulsoryDocuments 
    { 
     get { return compulsoryDocuments; } 
     set { compulsoryDocuments = value; } 
    } 

    public string Regulations 
    { 
     get { return regulations; } 
     set { regulations = value; } 
    } 

    public string Equipment 
    { 
     get { return equipment; } 
     set { equipment = value; } 
    } 

    public string SpeedLimitsLightVehicles 
    { 
     get { return speedLimitsLightVehicles; } 
     set { speedLimitsLightVehicles = value; } 
    } 

    public string AutoClubs 
    { 
     get { return autoClubs; } 
     set { autoClubs = value; } 
    } 
    } 

나는이 튜토리얼 사용 : http://www.geekchamp.com/tips/wp7-working-with-xml-reading-filtering-and-databinding 을하지만 난이 라인에 오류가 발생했습니다 :

  countriesList.ItemsSource = data; 

오류는 말한다 :

가 명시 적 변환이 존재

(당신이 누락 캐스트)?

WP7과 WP8의 LongListSelector가 동일한 컨트롤을 사용하지 않기 때문에 생각하지만 변경해야하는 항목을 알지 못하고 이에 대한 유용한 기사를 찾을 수 없습니다. 당신의 도움에 대한

감사합니다 :)

편집 :

  <!--Pivot item two--> 
     <phone:PivotItem Header="Countries"> 
      <!--Double line list no text wrapping--> 
      <phone:LongListSelector IsGroupingEnabled="False" x:Name="countriesList" Margin="0,0,-12,0" ItemsSource="{Binding Items}"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="0,0,0,17"> 
          <!--Image Width="60" Source="{Binding Photo}" Margin="12,6" HorizontalAlignment="Left"/--> 
          <TextBlock VerticalAlignment="Center" Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="82,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="Black"/> 
          <TextBlock VerticalAlignment="Center" Text="{Binding LineTwo}" TextWrapping="NoWrap" Margin="82,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="Black"/> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 
     </phone:PivotItem> 

답변

1

LongListSelector는 데이터 소스로 목록을 수행 할 수 있습니다 이것이 내가 데이터를 바인딩 할 것 XAML 코드입니다. 그냥 LINQ 쿼리에 .ToList() 메소드를 호출

countriesList.ItemsSource = data.ToList(); 

편집 :를 귀하의 코멘트에 대답하기 위해, 난 당신이 무슨 뜻인지 모르겠어요.

LongListSelector를 "Items"에 바인딩하므로 viewmodel의 "Items"속성에 국가 목록을 표시해야합니다. 그런 다음 LongListSelector의 ItemTemplate에서 컨트롤을 Country 클래스의 속성에 바인딩합니다.

<phone:PivotItem Header="Countries"> 
     <phone:LongListSelector IsGroupingEnabled="False" x:Name="countriesList" Margin="0,0,-12,0" ItemsSource="{Binding Items}"> 
      <phone:LongListSelector.ItemTemplate> 
       <DataTemplate> 
         <!-- Displays the name of the country --> 
         <TextBlock Text="{Binding Name}" /> 
       </DataTemplate> 
      </phone:LongListSelector.ItemTemplate> 
     </phone:LongListSelector> 
    </phone:PivotItem> 
+0

각 국가를 항목으로 만들려면 어떻게해야합니까? 또한 솔루션을 빌드하려고 할 때'XDocument xml = XDocument.Load ("/ Resources/EuroTrip.xml"); ' 시스템에'System.Xml.XmlException '유형의 예외가 발생했습니다. .Xml.ni.dll하지만 사용자 코드 – Skynet

+0

@ user2740682에서 처리되지 않았습니다. 각 국가는 자동으로 "항목"입니다. LongListSelector에 국가 목록을 지정하면 국가별로 항목이 만들어집니다. XML 오류의 경우 XML 파일의 빌드 조치를 "컨텐츠"로 설정하지 않았 음을 의미 할 수 있습니다. 또한 XML 파일이 유효하지 않을 수도 있습니다. 자세한 것은 내부 예외를 확인하십시오. –

+0

그것은 xap 패키지에서 파일을 찾을 수 없다고 말합니다 : http://grab.by/pU2q 하지만 "Content"에 있습니다 : http://grab.by/pU34 @KooKiz – Skynet