简体   繁体   中英

WP7 pivot control performance is NOT smooth for me

OK, my apps work, I just HATE the performance I'm seeing with switching pivot items. I randomly get stutters and hangups. I really suck at threading since I come from a web dev background. Is there something I could do differently to speed up my apps?

Here's the main page switching from my "Twist!" app. The biggest hangups come when switching between the "watch list" and the "my lists" items.

    private void panTwist_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        switch (panTwist.SelectedIndex)
        {
            case 0:     //Watch List
                App.vmTweet.LoadMore = false;
                DataContext = App.vmTweet;


                if (!App.vmTweet.IsWatchListTweetsLoaded)
                {
                    LoadWatchList(false);    
                }
                break;
            case 1:         //menu
                ApplicationBar = null;
                SetMenuDisplay();
                break;
            case 2:         //My Lists

                ApplicationBar = null;
                DataContext = App.vmTwitterList;
                if (!App.vmTwitterList.IsMyListsLoaded)
                {
                    GetMyLists();
                }
                MyListsSetDisplay();
                break;
            default:

                break;
        }

    }

Here's the code from my "Wiki-Reef" app. This one performs even worse....

    private void panCorals_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (panCorals.SelectedIndex)
        {
            case 0:     //search corals

                break;
            case 1:         //top corals
                if (!App.vmCoral.IsTopDataLoaded)
                {
                    App.vmCoral.IsTopLoading = true;
                    if (App.HasConnectivity)
                    {
                        //get corals from web
                        App.vmCoral.GetTopCorals();

                    }
                    else
                    {
                        //get saved corals from device
                        MessageBox.Show("Your phone does not have a connection to the internet, so the results you see could be empty or outdated.");
                        App.vmCoral.GetSavedTopCorals();
                    }
                }
                break;
            case 2:         //new corals

                if (!App.vmCoral.IsNewDataLoaded)
                {
                    App.vmCoral.IsNewLoading = true;
                    if (App.HasConnectivity)
                    {
                        //get corals from web
                        App.vmCoral.GetNewCorals();

                    }
                    else
                    {
                        //get saved corals from device
                        MessageBox.Show("Your phone does not have a connection to the internet, so the results you see could be empty or outdated.");
                        App.vmCoral.GetSavedNewCorals();
                    }
                }

                break;
            default:

                break;
        }

    }

I agree with Rico on this. This sounds like your making remote calls (calls to web services). If that's the case make sure you use HttpWebRequest instead of WebClient. When you use WebClient it blocks the UI thread. Straight from MSDN regarding using WebClient:

"the UI will be unresponsive until the processing is complete, causing a poor user experience, especially if the set of data being processed is large."

Here's the link . I recommend reading all of it as it contains other tips on increasing performance in your applications included on that page.

Don't reset the DataContext.

Try using this code. This follows MVVM (see http://msdn.microsoft.com/en-us/magazine/dd419663.aspx ) and doesn't reset the DataContext.

View model:

public class MainPageViewModel
{
    public MainPageViewModel()
    {
        ItemsOfPivotOne = new ObservableCollection<ItemOfPivotOne>();
        ItemsOfPivotTwo = new ObservableCollection<ItemOfPivotOne>();
    }

    public void LoadPivotOne()
    {
        // add your http logic here and add elements like this: 
        ItemsOfPivotOne.Add(item);
    }

    public void LoadPivotOne()
    {
        // add your http logic here and add elements like this: 
        ItemsOfPivotTwo.Add(item);
    }

    public ObservableCollection<ItemOfPivotOne> ItemsOfPivotOne {get; set;}
    public ObservableCollection<ItemOfPivotTwo> ItemsOfPivotTwo {get; set;}
}

Page code behind:

public class MainPage
{
    public MainPageViewModel Model { get { return (MainPageViewModel)Resources["viewModel"]; } }

    private void PivotSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (panTwist.SelectedIndex)
        {
            case 0:
                Model.LoadPivotOne();
                break;
            case 1: 
                Model.LoadPivotTwo();
                break;
        }
    }
}

XAML code with view model instantiation by resource:

<phone:PhoneApplicationPage x:Class="MyNamespace.MainPage" ... >
    <phone:PhoneApplicationPage.Resources>
        <viewModels:MainPageViewModel x:Key="viewModel" />
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" DataContext="{StaticResource viewModel}">
        <controls:Pivot Title="MY APPLICATION">
            <controls:PivotItem Header="Pivot 1">
                <ListBox ItemsSource="{Binding ItemsOfPivotOne}" />
            </controls:PivotItem>
            <controls:PivotItem Header="Pivot 2">
                <ListBox ItemsSource="{Binding ItemsOfPivotTwo}" />
            </controls:PivotItem>
        ...

I hope this helps... if not ask me here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM