在我的应用程序中,我有一个由 TextBox、Listpicker 和 LongListSelector 组成的屏幕。
LongListSelector 包含定义类别的地点列表。
我们可以通过从 listpicker 中选择所需的类别或在文本框中键入文本来对列表进行排序,这将搜索列表并为您提供所需的列表。
我有 10 个类别,我的代码适用于 9 个类别,但如果是第 10 个类别,当我选择然后在搜索框中输入任何内容然后将其删除时,我的应用程序就会崩溃。
我在任何地方都尝试过尝试捕获,但它直接进入调试器,中断并且它在调试器中显示的异常是
“检测到布局周期。布局无法完成。”
我的xml代码是:
<phone:PhoneApplicationPage.Resources>
<local:Number x:Key="Number"/>
<DataTemplate x:Name="PickerFullItemTemplate">
<StackPanel>
<TextBlock Text="{Binding}" FontSize="36"/>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="White" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<Grid Grid.Row="0" Background="#132d63">
<TextBlock Foreground="white" Text="{Binding Path=LocalizedResources.Places,Source={StaticResource LocalizedStrings}}" Margin="0,6,0,8" FontSize="40" HorizontalAlignment="Center"/>
</Grid>
<Grid Grid.Row="1" >
<toolkit:PhoneTextBox Hint="{Binding Path=LocalizedResources.Search,Source={StaticResource LocalizedStrings}}" Text="{Binding searchtextbox,Mode=TwoWay,UpdateSourceTrigger=Default}" TextChanged="Searchbox_TextChanged" Name="Searchbox" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Stretch" Background="White" BorderBrush="Black" BorderThickness="1"/>
</Grid>
<Grid Grid.Row="2" Margin="12,0,12,0">
<toolkit:ListPicker ExpansionMode="ExpansionAllowed" FullModeHeader="Categories"
Name="listpicker"
ItemsSource="{Binding Categories,Mode=TwoWay,UpdateSourceTrigger=Default}" FullModeItemTemplate="{StaticResource PickerFullItemTemplate}" SelectedItem="{Binding listpickerselecteditem,Mode=TwoWay,UpdateSourceTrigger=Default}" BorderBrush="Black" BorderThickness="1" Background="White" Foreground="Black" Margin="0,-5,0,0"/>
</Grid>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="3" Margin="12,0,0,0">
<ls:LongListSelector1 x:Name="placesinfolist" Margin="0,10,0,10" ItemsSource="{Binding attractionlist,Mode=TwoWay,UpdateSourceTrigger=Default}" SelectedItem="{Binding SelectedAttractionDetail,Mode=TwoWay,UpdateSourceTrigger=Default}" />
</Grid>
</Grid>
我的代码视图模型是
public class AttractionViewModel : ViewModelBase
{
List<AttractionDetails> newsearchlist = new List<AttractionDetails>();
Boolean Isgetattractionlist = false;
#region Properties
ObservableCollection<AttractionDetails> attractionlistbackup { get; set; }
private ObservableCollection<AttractionDetails> _attractionlist;
public ObservableCollection<AttractionDetails> attractionlist
{
get
{
return _attractionlist;
}
set
{
_attractionlist = value;
RaisePropertyChanged("attractionlist");
}
}
private bool _isLoading;
public bool IsLoading
{
get
{
return _isLoading;
}
private set
{
_isLoading = value;
RaisePropertyChanged("IsLoading");
}
}
private AttractionDetails _SelectedAttractionDetail;
public AttractionDetails SelectedAttractionDetail
{
get
{
return _SelectedAttractionDetail;
}
set
{
int index = 0;
_SelectedAttractionDetail = value;
RaisePropertyChanged("SelectedAttractionDetail");
for (int i = 0; i < attractionlist.Count(); i++)
{
if (attractionlist[i] == SelectedAttractionDetail)
{
index = i;
break;
}
}
Messenger.Default.Send<int>(index, "toattractiondetails");
IsLoading = true;
Messenger.Default.Send<ObservableCollection<AttractionDetails>>(attractionlist, "attractiondetails");
}
}
private List<string> _Categories;
public List<string> Categories
{
get
{
return _Categories;
}
set
{
_Categories = value;
}
}
private string _searchtextbox;
public string searchtextbox
{
get
{
return _searchtextbox;
}
set
{
try
{
_searchtextbox = value;
// RaisePropertyChanged("searchtextbox");
if (attractionlist == null)
attractionlist = new ObservableCollection<AttractionDetails>();
attractionlist.Clear();
if (listpickerselecteditem == null)
{
listpickerselecteditem = AppResources.All;
}
if (listpickerselecteditem == AppResources.All)
{
attractionlist.Clear();
foreach (var item in attractionlistbackup)
{
if (!(item.name.ToLowerInvariant().IndexOf(searchtextbox) == -1))
{
attractionlist.Add(item);
}
}
}
else
{
attractionlist.Clear();
foreach (var item in attractionlistbackup)
{
string qwe = "";
qwe = item.categoryType;
qwe = qwe.Replace("\r\n", string.Empty);
if (!(item.name.ToLowerInvariant().IndexOf(searchtextbox) == -1) && listpickerselecteditem == qwe)
{
attractionlist.Add(item);
}
}
}
}
catch (Exception e)
{
}
}
}
private string _listpickerselecteditem;
public string listpickerselecteditem
{
get
{
return _listpickerselecteditem;
}
set
{
_listpickerselecteditem = value;
RaisePropertyChanged("listpickerselecteditem");
if (attractionlist == null)
attractionlist = new ObservableCollection<AttractionDetails>();
attractionlist.Clear();
if (listpickerselecteditem == AppResources.All)
{
foreach (var item in attractionlistbackup)
{
if (searchtextbox != null)
{
if (!(item.name.ToLowerInvariant().IndexOf(searchtextbox) == -1))
{
attractionlist.Add(item);
}
}
else
{
attractionlist.Add(item);
}
}
}
else
{
foreach (var item in attractionlistbackup)
{
string qwe = "";
qwe = item.categoryType;
qwe = qwe.Replace("\r\n", string.Empty);
if (searchtextbox == null)
{
if (listpickerselecteditem == qwe)
attractionlist.Add(item);
}
else if (!(item.name.ToLowerInvariant().IndexOf(searchtextbox) == -1) && listpickerselecteditem == qwe)
{
attractionlist.Add(item);
}
}
}
}
}
#endregion
public AttractionViewModel()
{
Messenger.Default.Register<string>(this, "attraction", GetAttractions);
Messenger.Default.Register<string>(this, "toplacesview", setlist);
}
public void setlist(string message)
{
if (searchtextbox != message)
{
searchtextbox = message;
}
}
public void GetAttractions(string message)
{
}
}
而我背后的代码是
public partial class Attractions : PhoneApplicationPage
{
public Attractions()
{
InitializeComponent();
}
private void Searchbox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
TextBox asd = sender as TextBox;
Messenger.Default.Send<string>(asd.Text, "toplacesview");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GotoAttractionDetails(object sender, System.Windows.Input.GestureEventArgs e)
{
AttractionDetailsPage.flag = 0;
NavigationService.Navigate(new Uri("/Skins/AttractionDetailsPage.xaml", UriKind.Relative));
}
}
每次文本更改时都会触发两次 textchanged 事件,我不知道为什么会这样。
请帮忙.....