我举了一个简短的例子来说明我遇到了一些问题。他们将LongsListSelector绑定到ObservableCollection(无论是什么类型的项目)。我将长列表项模板设置为(例如)一个文本块,我还制作了三个按钮 - 将一个元素添加到集合中,删除最后一个元素,以及搜索visuall树。代码不是很长,所以我将在下面发布它(如果你想要整个例子,那就是:http://sdrv.ms/163TYEG)。
XAML(标题除外):
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="ElementStyle">
<TextBlock x:Name="elemBlck" Text="Element"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="30*"/>
<RowDefinition Height="30*"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<Button x:Name="addBtn" Content="Add" Grid.Row="0" Grid.Column="1"/>
<Button x:Name="delBtn" Content="Del" Grid.Row="1" Grid.Column="1"/>
<Button x:Name="showBtn" Content="Show" Grid.Row="2" Grid.Column="1"/>
<phone:LongListSelector x:Name="phoneLLS" HorizontalAlignment="Left" Height="700" Margin="0" Grid.Row="0"
VerticalAlignment="Top" Grid.RowSpan="3" Grid.Column="0" Width="300"
LayoutMode="Grid" GridCellSize="300,100" IsGroupingEnabled="False"
ItemTemplate="{StaticResource ElementStyle}" />
</Grid>
和C#:
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<string> collection = new ObservableCollection<string>();
public MainPage()
{
InitializeComponent();
phoneLLS.ItemsSource = collection;
addBtn.Click += addBtn_Click;
delBtn.Click += delBtn_Click;
showBtn.Click += showBtn_Click;
}
private void addBtn_Click(object sender, RoutedEventArgs e)
{
collection.Add("element");
}
private void delBtn_Click(object sender, RoutedEventArgs e)
{
collection.RemoveAt(collection.Count - 1);
}
private void showBtn_Click(object sender, RoutedEventArgs e)
{
List<TextBlock> controlList = new List<TextBlock>();
SearchForControls<TextBlock>(phoneLLS, ref controlList);
}
private static void SearchForControls<T>(DependencyObject parent, ref List<T> controlList) where T : DependencyObject
{
int numberOfChildreen = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numberOfChildreen; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
controlList.Add((T)child);
else SearchForControls<T>(child, ref controlList);
}
}
}
问题在哪里?
当我按下“添加”按钮时,该集合会被一个元素放大。好的,我看到它因为LLS中添加了元素项。但试着把它推得更多。之后在一个按钮内推出几个元素?怎么了?集合由一个元素放大,但LLS的填充速度要快得多。
与删除相同 - 它会删除一个元素,但是从LLS中删除一组项目有时会消失。
主要问题 - 在showBtn_Click的第二行切换断点。添加一些元素,尝试搜索可视树(按下显示按钮)。调试停止,在第二行之后,您可以看到可视树中元素的确切数字(与collection.Count不同)。第二个大惊喜 - 当你删除en元素,并再次搜索可视树时,你会看到:集合已经下降(这是正确的),一些元素从LLS中消失(应该是一个),并且! - Visual树元素的数量没有改变(!)。怎么了?
这些是某种错误吗?或者也许我不明白?
答案 0 :(得分:0)
尝试将数据上下文设置为List并使用ItemsSource属性中的Bindings。
的Xaml:
<phone:LongListSelector ItemsSource={Binding} x:Name="phoneLLS" HorizontalAlignment="Left" Height="700" Margin="0" Grid.Row="0"
VerticalAlignment="Top" Grid.RowSpan="3" Grid.Column="0" Width="300"
LayoutMode="Grid" GridCellSize="300,100" IsGroupingEnabled="False"
ItemTemplate="{StaticResource ElementStyle}" />
代码背后:
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<string> collection = new ObservableCollection<string>();
public MainPage()
{
InitializeComponent();
phoneLLS.DataContext = collection;
addBtn.Click += addBtn_Click;
delBtn.Click += delBtn_Click;
showBtn.Click += showBtn_Click;
}
...
}
让我知道这是否有效。
答案 1 :(得分:0)
我还做了一些进一步的研究 - 如果你用LLS重新进入页面,一切都更新正确 - 视觉树有许多元素,如集合。我修改了一个示例(http://sdrv.ms/169kRqI):
让Main_Page成为second_page:
的Xaml:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="ElementStyle">
<TextBlock x:Name="elemBlck" Text="Element"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="30*"/>
<RowDefinition Height="30*"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<Button x:Name="addBtn" Content="Add" Grid.Row="0" Grid.Column="1"/>
<Button x:Name="delBtn" Content="Del" Grid.Row="1" Grid.Column="1"/>
<Button x:Name="showBtn" Content="Show" Grid.Row="2" Grid.Column="1"/>
<phone:LongListSelector x:Name="phoneLLS" HorizontalAlignment="Left" Height="700" Margin="0" Grid.Row="0"
VerticalAlignment="Top" Grid.RowSpan="3" Grid.Column="0" Width="300"
LayoutMode="Grid" GridCellSize="300,100" IsGroupingEnabled="False"
ItemTemplate="{StaticResource ElementStyle}" ItemsSource="{Binding}"/>
</Grid>
代码背后的代码:
public partial class second_page : PhoneApplicationPage
{
public second_page()
{
InitializeComponent();
phoneLLS.DataContext = MainPage.collection;
addBtn.Click += addBtn_Click;
delBtn.Click += delBtn_Click;
showBtn.Click += showBtn_Click;
}
private void addBtn_Click(object sender, RoutedEventArgs e)
{
MainPage.collection.Add("element");
}
private void delBtn_Click(object sender, RoutedEventArgs e)
{
MainPage.collection.RemoveAt(MainPage.collection.Count - 1);
}
private void showBtn_Click(object sender, RoutedEventArgs e)
{
List<TextBlock> controlList = new List<TextBlock>();
SearchForControls<TextBlock>(phoneLLS, ref controlList);
}
private static void SearchForControls<T>(DependencyObject parent, ref List<T> controlList) where T : DependencyObject
{
int numberOfChildreen = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numberOfChildreen; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
controlList.Add((T)child);
else SearchForControls<T>(child, ref controlList);
}
}
}
和Main_Page - 仅导航:
的Xaml:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Button x:Name="goToPage" Content="SecondPage" Width="Auto" Height="Auto"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
代码背后:
public partial class MainPage : PhoneApplicationPage
{
public static ObservableCollection<string> collection = new ObservableCollection<string>();
public MainPage()
{
InitializeComponent();
goToPage.Click+=goToPage_Click;
}
private void goToPage_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/second_page.xaml", UriKind.Relative));
}
}
尝试在重新输入页面时尝试相同操作:
添加例如3个元素,调试器在showBtn_Click的第二行切换断点并单击按钮 - 它将显示6个元素!除了回去,不要碰任何东西。
重新进入第二页 - 您将获得正确的LLS - 当您单击带断点的showBtn时,您将看到3个元素,您将在可视树中看到3个元素。
有人试过这个吗?