导航期间多次调用ListPicker SelectionChanged事件 [英] ListPicker SelectionChanged Event Called Multiple Times During Navigation

查看:77
本文介绍了导航期间多次调用ListPicker SelectionChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个应用程序页面中的ListPicker中有一个SelectionChanged事件,该事件在加载页面之前会触发多次.这样实在太不方便了我,当一个项目被选中的,将显示一个MessageBox(和其他操作将执行).每次页面导航到时,MessageBox都会显示两次.我该如何解决?

I have a SelectionChanged event in a ListPicker within one of my application Pages that fires multiple times before the page is loaded. This is really inconvenient for me as when an item is selected, a MessageBox is displayed (and other actions will be performed). The MessageBox is displayed twice every time the page is NavigatedTo. How can I fix this?

XAML

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme"
                    ItemTemplate="{StaticResource PickerItemTemplate}" 
                    SelectionChanged="ThemeListPicker_SelectionChanged"/>

XAML.CS

private void ThemeListPicker_SelectionChanged(object sender,
                                              SelectionChangedEventArgs e)
{
   if(ThemeListPicker.SelectedIndex != -1)
   {
       var theme = (sender as ListPicker).SelectedItem;

       if (index == 0)
       {
          Settings.LightTheme.Value = true;
          MessageBox.Show("light");
       }
       else
       {
           Settings.LightTheme.Value = false;
           MessageBox.Show("dark");
       }
   }                            
}

推荐答案

好吧,这就是列表选择器的行为,您最好的办法就是不要让ThemeListPicker_SelectionChanged像这样在数据模板中创建父堆栈面板

well, that's how a listpicker behaves, what best you can do is instead of making ThemeListPicker_SelectionChanged make a parent stackpanel inside the datatemplate somewhat like this

<Listpicker.ItemTemplate>
<DataTemplate x:Name="PickerItemTemplate"> 
               <StackPanel tap="stk_Tap"> 
                    <TextBlock/> 
                </StackPanel> 
            </DataTemplate>
</Listpicker.ItemTemplate>
<Listpicker.FullModeItemTemplate>
            <DataTemplate x:Name="PickerFullModeItemTemplate"> 
                <StackPanel tap="stk_Tap"> 
                    <TextBlock/> 
                </StackPanel> 
            </DataTemplate>
<Listpicker.FullModeItemTemplate>

现在使用此水龙头stk_Tap进行操作,因为每次选择更改被调用时也会调用此事件,但是,它不会表现出像选择更改事件那样的错误行为.

now use this tap stk_Tap to do your action as, this event would also get called every time the selection changed gets called but, it wont exhibit the buggy behavior like that of selection changed event.

希望这会有所帮助.

这篇关于导航期间多次调用ListPicker SelectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆