Turn control shows one Pivot, but registers cranes in another

I have todolist application where there are a few controls ListBoxwithin an individual PivotItemsinside the control Pivot. If I go to another page and return using the back button, the displayed one Pivotdoes not respond. However, the activity on it (scrolling, slicing, etc.) seems to affect the following Pivot, i.e. SelectedIndex + 1;

It sounds very much like an error in the control Pivot, but maybe I'm doing something wrong? Here is the code for my rotary control.

[code updated, see below]

I don’t think that the code of the page I'm going to is very important, because the behavior is done using the back button

Other important information:

  • At the very beginning PivotItem(index 0), the error is not executed .
  • In the very last PivotItem, another problem arises . Going from the last list and returning with the "Back" button shows an empty list .
  • In all cases, the problem is resolved by moving to the list left or right, and then returning.

EDIT:

I was mistaken that the page code is not important. I am attached to a method OnNavigatedTo, not a method MainPage. Romasz provided a sample that worked perfectly. I broke it by changing the three parts according to my own broken code.

  • Set page to inherit from INotifyPropertyChanged(as I use NotifyPropertyChanged)
  • Moved data binding code to method OnNavigatedTo
  • Set ItemsSource PivotControlin code

. .

MainPage.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid x:Name="ContentPanel" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="9*"/>
        </Grid.RowDefinitions>
        <Button x:Name="myButton" VerticalAlignment="Top" 
             Content="Go to different Page!" Grid.Row="0"/>
        <controls:Pivot x:Name="PivotControl" Grid.Row="1" 
             ItemsSource="{Binding Lists}">
            <controls:Pivot.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </controls:Pivot.HeaderTemplate>
            <controls:Pivot.ItemTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Items}" 
            HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" 
            Margin="24,0,0,0" Height="Auto" VerticalAlignment="Stretch">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox></CheckBox>
                                    <TextBlock Text="{Binding Title}"/>
                                </StackPanel>                                    
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </controls:Pivot.ItemTemplate>
        </controls:Pivot>
    </Grid>
</Grid>

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
    public class myItems
    {
        public string Title { get; set; }
    }

    public class myList
    {
        public string Name { get; set; }
        private List<myItems> items = new List<myItems>();
        public List<myItems> Items
        {
            get { return items; }
            set { items = value; }
        }
    }

    private ObservableCollection<myList> _Lists;
    public ObservableCollection<myList> Lists
    {
        get
        {
            return _Lists;
        }
        set
        {
            if (_Lists != value)
            {
                _Lists = value;
                NotifyPropertyChanged("Lists");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        Lists = new ObservableCollection<myList>();

        myList list1 = new myList() { Name = "First" };
        for (int i = 0; i < 100; i++)
            list1.Items.Add(new myItems() { Title = (i + 1).ToString() });

        myList list2 = new myList() { Name = "Second" };
        for (int i = 100; i < 200; i++)
            list2.Items.Add(new myItems() { Title = (i + 1).ToString() });

        myList list3 = new myList() { Name = "Third" };
        for (int i = 200; i < 300; i++)
            list3.Items.Add(new myItems() { Title = (i + 1).ToString() });

        myList list4 = new myList() { Name = "Fourth" };
        for (int i = 300; i < 400; i++)
            list4.Items.Add(new myItems() { Title = (i + 1).ToString() });

        Lists.Add(list1);
        Lists.Add(list2);
        Lists.Add(list3);
        Lists.Add(list4);

        myButton.Click += first_Click;
        this.DataContext = this;

    }

    private void first_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));
    }
}

: https://dl.dropboxusercontent.com/u/568631/PivotProblem_2.zip

, . , " ", "", "" . .

- INotifyPropertyChanged, MainPage ItemsSource codebehind - , . (- ), , , , PivotControl

, - ?

+4
2

- , , ,

    bool IsDataLoaded = false; //flag

    public MainPage()
    {
        InitializeComponent();

    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        base.OnNavigatedTo(e);

        if (!IsDataLoaded)
        {
            Lists = new ObservableCollection<myList>();

            myList list1 = new myList() { Name = "First" };
            for (int i = 0; i < 100; i++)
                list1.Items.Add(new myItems() { Title = (i + 1).ToString() });

            myList list2 = new myList() { Name = "Second" };
            for (int i = 100; i < 200; i++)
                list2.Items.Add(new myItems() { Title = (i + 1).ToString() });

            myList list3 = new myList() { Name = "Third" };
            for (int i = 200; i < 300; i++)
                list3.Items.Add(new myItems() { Title = (i + 1).ToString() });

            myList list4 = new myList() { Name = "Fourth" };
            for (int i = 300; i < 400; i++)
                list4.Items.Add(new myItems() { Title = (i + 1).ToString() });

            Lists.Add(list1);
            Lists.Add(list2);
            Lists.Add(list3);
            Lists.Add(list4);

            myButton.Click += first_Click;
            this.DataContext = this;
            IsDataLoaded = true;
        }

    }

OnNavigatedTo , . (:

0

, . Pivot 4 , texblocks. , .

:

  • PivotItem, , " ", "" = "" "" rensponsive
  • PivotItem , , , , Back = > Pivot List rensponsive
  • PivotItem, PivotItem , , , PivotItem , , , , Back = > Pivot List rensponsive

( WP8). ? - ?

EDIT -

, , . , ( ). ( ):

, ( OnNavigatedTo), , :

while (Lists.Count > 1)
   Lists.RemoveAt(1);
// add new items
Lists.RemoveAt(0); // watch out for the first run of OnNavigatedTo - there is a need here for if statement or something

, , , , , . , .

, , .

0

Source: https://habr.com/ru/post/1525028/


All Articles