简体   繁体   English

WP7枢轴控制性能对我来说并不顺利

[英]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. 我真的很喜欢线程,因为我来自web开发背景。 Is there something I could do differently to speed up my apps? 有什么我可以做的不同,以加快我的应用程序?

Here's the main page switching from my "Twist!" 这是从“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. 这是我的“Wiki-Reef”应用程序的代码。 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. 我同意Rico的观点。 This sounds like your making remote calls (calls to web services). 这听起来像是在进行远程调用(调用Web服务)。 If that's the case make sure you use HttpWebRequest instead of WebClient. 如果是这种情况,请确保使用HttpWebRequest而不是WebClient。 When you use WebClient it blocks the UI thread. 使用WebClient时,它会阻止UI线程。 Straight from MSDN regarding using WebClient: 直接从MSDN关于使用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. 不要重置DataContext。

Try using this code. 尝试使用此代码。 This follows MVVM (see http://msdn.microsoft.com/en-us/magazine/dd419663.aspx ) and doesn't reset the DataContext. 这遵循MVVM(请参阅http://msdn.microsoft.com/en-us/magazine/dd419663.aspx )并且不重置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: 具有资源的视图模型实例化的XAML代码:

<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... 我希望这有帮助...如果不在这里问我......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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