在我的 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;
}
}
在调试时,我在设置为列表之前检查了代码隐藏文件中的变量List Companies变量,并且我正在正确获取数据。但是数据没有绑定到列表。我不知道为什么数据没有绑定。
现在测试我已经用静态文本替换了绑定。然后我运行应用程序,页面中也看不到数据。但是在列表项更改时,我正在获取数据。这意味着我的 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