2013-07-01 58 views
0

在我的Windows Phone 8应用程序中,当用户单击按钮时,我打电话给Web服务,并将结果显示在List类型的变量中。 之后,我试图将结果绑定到xaml页面中的列表。但数据不可见。数据在列表中不可见

//Data Context object 
class Company 
    {  
    public string CompanyId { get; set; } 

    public string CompanyName { get; set; } 

    public string CityId { get; set; } 

    public string Category { get; set; } 

    public string CategoryId { get; set; } 

    public string Address { get; set; } 

    public string Phone1 { get; set; } 

    public string Mobile1 { get; set; } 

    public string Email { get; set; } 

    public string Fax { get; set; } 

    public string Website { get; set; } 

    public string Profile { get; set; } 

    public string Latitude { get; set; } 

    public string Longitude { get; set; } 

    public string Sponsored { get; set; } 
} 

在我的XAML页面中,我将上面的结果绑定到List。

<Grid x:Name="ContentPanel" Grid.Row="3" Background="White" Margin="0,-3,0,0"> 
     <ListBox x:Name="companiesList" 
       SelectionChanged="companiesList_SelectionChanged"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid x:Name="listItem"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="40"/> 
          <RowDefinition Height="30"/> 
          <RowDefinition Height="30"/> 
          <RowDefinition Height="10"/> 
         </Grid.RowDefinitions> 

         <TextBlock x:Name="nameTextBlock" Grid.Row="0" Text="{Binding CompanyName}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MaxHeight="40" TextTrimming="WordEllipsis" Margin="5,0,0,5"/> 

         <TextBlock x:Name="addressTextBlock" Grid.Row="1" Text="{Binding Address}" Foreground="#FF1F1F1F" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis" Margin="5,0,0,5"/> 

         <StackPanel x:Name="addressPanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,5"> 
          <Image x:Name="phone" Stretch="Uniform" Margin="0,0,5,0" Height="25" Source="Images/list_phone.png" /> 
          <TextBlock x:Name="phoneTextBlock" Text="{Binding Phone1}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis"/> 
         </StackPanel> 

         <Image x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png" /> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

之后,在代码背后的文件,我像下面的绑定。

private void CompaniesPage_Loaded(object sender, RoutedEventArgs e) 
    { 

     if (isPageAlreadyLoaded == false) 
     {     
      List<Company> companies = (List<Company>)DataContext; 
      companiesList.ItemsSource = companies; 
      isPageAlreadyLoaded = true; 
     } 
    } 

在调试我检查变量上市公司变量在代码隐藏文件设置为列出之前,我正确获取数据。 但数据没有绑定到列表。我不知道为什么数据没有约束力。

现在来测试我的替换了静态文本的绑定。然后我运行应用程序,然后数据在页面中不可见。 但在列表项更改我收到的数据。这意味着我的xaml页面无法显示数据。那么你能否告诉我在我下面的xaml页面设计中出了什么问题。

<phone:PhoneApplicationPage 
x:Class="STCDirectory.CompaniesPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
mc:Ignorable="d" 
shell:SystemTray.IsVisible="True" Loaded="CompaniesPage_Loaded" 
xmlns:my="clr-namespace:System.Windows.Controls;assembly=WindowsPhoneWatermarkTextBoxControl"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot"> 
    <Grid.Background> 
     <ImageBrush Stretch="UniformToFill" ImageSource="/STCDirectory;component/Images/search_list_bg.png" /> 
    </Grid.Background> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="80"/> 
     <RowDefinition Height="80"/> 
     <RowDefinition Height="80"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Orientation="Vertical" Margin="0,0,0,10" > 
     <TextBlock x:Name="header" Text="STC Directory" HorizontalAlignment="Left" Margin="10,20,0,5" Foreground="#FF501F6E" FontWeight="Bold" FontSize="35" /> 
    </StackPanel> 
    <StackPanel x:Name="buttonsBar" Grid.Row="1" Orientation="Horizontal" Margin="0,10,0,0"> 
     <Button Content="Button" Height="70" HorizontalAlignment="Left" Margin="14,1,0,0" Name="button1" VerticalAlignment="Top" Width="160" /> 
     <Button Content="Button" Height="70" HorizontalAlignment="Left" Margin="130,1,0,0" Name="button2" VerticalAlignment="Top" Width="160" /> 
    </StackPanel> 
    <StackPanel x:Name="searchBar" Grid.Row="2" Orientation="Horizontal" > 
     <my:WatermarkTextBox Name="textBlock1" Width="400" Margin="-5,0,-10,0" WatermarkText="{Binding Path=LocalizedResources.SearchHint, Source={StaticResource LocalizedStrings}}" TextWrapping="Wrap" Foreground="Black" TextAlignment="Left" BorderBrush="{x:Null}" FontSize="25"> 
      <my:WatermarkTextBox.Background> 
       <ImageBrush ImageSource="/STCDirectory;component/Images/search_box.png" /> 
      </my:WatermarkTextBox.Background> 
     </my:WatermarkTextBox> 
     <Button x:Name="serchButton" Style ="{StaticResource ButtonStyleIB}" VerticalAlignment="Center" Height="70" Click="serach_button_clicked"> 
      <Image Source="/STCDirectory;component/Images/search.png" Stretch="Fill" /> 
     </Button> 
    </StackPanel> 
    <Grid x:Name="ContentPanel" Grid.Row="3" Background="White" Margin="0,-3,0,0"> 
     <ListBox x:Name="companiesList" 
       SelectionChanged="companiesList_SelectionChanged"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid x:Name="listItem"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="40"/> 
          <RowDefinition Height="30"/> 
          <RowDefinition Height="30"/> 
          <RowDefinition Height="10"/> 
         </Grid.RowDefinitions> 

         <TextBlock x:Name="nameTextBlock" Grid.Row="0" Text="Kentuc Fried Chicken(KFC)" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MaxHeight="40" TextTrimming="WordEllipsis" Margin="5,0,0,5"/> 

         <TextBlock x:Name="addressTextBlock" Grid.Row="1" Text="Al riyadh" Foreground="#FF1F1F1F" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis" Margin="5,0,0,5"/> 

         <StackPanel x:Name="addressPanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,5"> 
          <Image x:Name="phone" Stretch="Uniform" Margin="0,0,5,0" Height="25" Source="Images/list_phone.png" /> 
          <TextBlock x:Name="phoneTextBlock" Text="966123456" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="30" TextTrimming="WordEllipsis"/> 
         </StackPanel> 

         <Image x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png" /> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Grid> 

我期待着你的回应 谢谢, Basina

+0

我不记得它是否需要在Windows Phone上,但为了防止我建议将公司类设置为public:'public class Company' –

+0

hi Kookiz,我已将该类更改为public,然后页面不显示数据。 –

回答

0
public MainPage() 
    { 
     this.InitializeComponent(); 
     List<Company> companies = new List<Company>(); 
     Company comp = new Company(); 
     comp.CompanyName = "hellocpmp1"; 
     companies.Add(comp); 
     companies.Add(comp); 
     companies.Add(comp); 
     companies.Add(comp); 
     companies.Add(comp); 

     companies.Add(comp); 
     companies.Add(comp); 

     companies.Add(comp); 

     companies.Add(comp); 
     companies.Add(comp); 
     companies.Add(comp); 



     companiesList.ItemsSource = companies; 
    } 

我已经尝试过了类似上面,它是在你的情况THR可能是问题的工作如此精细没有来自Web服务的数据。

+0

在我提到的最后一行的问题中,我从Web服务获取数据,并将数据传递到已成功列出项目的页面。而且我在将它分配给列表之前检查数据是否到来。 –

+0

但我已经试过你的代码它来了,所以你可以做的只是尝试根据我做了什么,并检查是否根据我这是对还是错..我也删除了你的文本框样式。 – loop

+0

嗨Tanuj,我编辑了我的问题。请在问题中找到我的整个UI页面代码。我用静态文本替换了绑定选项,并运行应用程序。然后,静态文本在页面中不可见。所以这意味着我的xaml页面出现了一些问题。我没有发现确切的错误。你可以用静态文本来测试这个页面吗?我期待着您的回复。谢谢巴西娜 –

0
<phone:PhoneApplicationPage 
x:Class="PhoneApp1.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:toolkit="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit.Controls" 
mc:Ignorable="d" 



shell:SystemTray.IsVisible="True"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="ContentPanel" Background="Transparent" > 
    <ListBox x:Name="companiesList" ItemsSource="{Binding companies}" 
      SelectionChanged="companiesList_SelectionChanged_2"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 

       <Grid x:Name="listItem"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="40"/> 
         <RowDefinition Height="30"/> 
         <RowDefinition Height="30"/> 
         <RowDefinition Height="10"/> 
        </Grid.RowDefinitions> 

        <TextBlock x:Name="nameTextBlock" Grid.Row="0" Text="{Binding CompanyName}" Foreground="#FF501F6E" /> 

        <TextBlock x:Name="addressTextBlock" Grid.Row="1" Text="Al riyadh" Foreground="#FF1F1F1F" /> 

        <StackPanel x:Name="addressPanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,5"> 
         <Image x:Name="phone" Stretch="Uniform" Margin="0,0,5,0" Height="25" Source="Images/list_phone.png" /> 
         <TextBlock x:Name="phoneTextBlock" Text="966123456" Foreground="#FF501F6E" /> 
        </StackPanel> 

        <Image x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png" /> 
       </Grid> 



      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

我发现你的问题THR是出错了的TextBlocks的你的风格的一部分,所以我已删除了它。我已经把一些工作代码绑定以及硬编码文本。告诉我你的问题是否解决了。

+0

嗨朋友, 感谢您的所有回应。我已经解决了这个问题,将文本块放在stackpanel中,并将宽度属性放置到堆栈面板,如下所示。

+0

我告诉过你thr是布局部分的问题,绑定部分没有错。 – loop