在Windows Phone上渲染ListBox需要太长时间

时间:2012-11-09 20:26:15

标签: listbox windows-phone

我正在使用本地SQLite数据库处理Windows Phone 7应用程序,而且我对使用DataBinding的页面的呈现时间有疑问。

目前从数据库中检索数据需要60-70ms。然后,使用带有DataBinding的ListBox渲染数据需要大约3100ms。

在这里您可以看到ListBox的DataTemplate:

<DataTemplate x:Key="ListBoxItemTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="68" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock x:Name="TimeColumn"
                        Text="{Binding TimeSpan}" Grid.Column="0" Grid.Row="0"
                        Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBlock Text="{Binding Stop.StopName}" Grid.Column="1" Grid.Row="0" 
                                Margin="15,0,0,0" TextWrapping="NoWrap" Foreground="Black"
                                HorizontalAlignment="Left" VerticalAlignment="Center" />
    </Grid>
 </DataTemplate>

评论:我也尝试使用Canvas而不是Grid,结果相同。

然后,数据库将数据加载到CSList中(使用ViciCoolStorage)并将其绑定到ListBox:

StationList.ItemsSource = App.RouteViewModel.RouteStops;

评论:我试图将CSList的元素添加到ObservableCollection并将其绑定到接口但似乎没有任何改变。

问题: 我做错了会导致巨大的加载时间 - 即使只加载10个元素 - 或者这是正常的吗?您是否有任何建议可以使用DataBinding获得更好的性能?

提前感谢您的回答!

相应的代码部分:

RouteViewModel.cs

private Route rRoute;
public Route Route
{
    get
    {
        if (rRoute != null)
        {
            return rRoute;
        }
        else
        {
            return new Route();
        }
    }
}

public void LoadRoute(string index)
{
    try
    {
        if (rRoute.RouteId != index)
        {
            RouteLoaded = false;
            StationsLoaded = false;
            TimetableLoaded = false;
        }
    }
    catch (Exception) { }

    this.index = index;

    if (!RouteLoaded)
    {
        NotifyPropertyChanging("Route");
        rRoute = Route.ReadSafe(index);
        RouteLoaded = true;
        NotifyPropertyChanged("Route");
    }
}

private CSList<RouteTime> rtLine;
public CSList<RouteTime> RouteStops
{
    get
    {
        if (rtLine != null)
        {
            return rtLine;
        }
        else
        {
            return new CSList<RouteTime>();
        }
    }
}

public void LoadRouteStops()
{
    LoadRoute(index);

    if (!this.StationsLoaded)
    {
        NotifyPropertyChanging("RouteStops");
        rtLine = rRoute.RouteTimes.FilteredBy("DirectionId = @DirectionId", "@DirectionId", this.direction).OrderedBy("TimeSpan");
        NotifyPropertyChanged("RouteStops");

        StationsLoaded = true;
    }
}

RouteView.xaml.cs

private string index;
private bool visszaut = false;

public RouteView()
{
    InitializeComponent();
    Loaded += new System.Windows.RoutedEventHandler(RouteView_Loaded);
}

void RouteView_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    DataContext = App.RouteViewModel;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    NavigationContext.QueryString.TryGetValue("index", out index);

    App.RouteViewModel.LoadRoute(index);
    App.RouteViewModel.Direction = Convert.ToInt32(visszaut);
    App.RouteViewModel.LoadRouteStops();
    StationList.ItemsSource = App.RouteViewModel.RouteStops;
}

RouteTime.cs - 班级实施

[MapTo("RouteTimes")]
public class RouteTime : CSObject<RouteTime, int>
{
    public int RouteTimeId
    {
        get
        {
            return (int)GetField("RouteTimeId");
        }
        set
        {
            SetField("RouteTimeId", value);
        }
    }

    public int RouteId
    {
        get
        {
            return (int)GetField("RouteId");
        }
        set
        {
            SetField("RouteId", value);
        }
    }

    public int StopId
    {
        get
        {
            return (int)GetField("StopId");
        }
        set
        {
            SetField("StopId", value);
        }
    }

    public int TimeSpan
    {
        get
        {
            return (int)GetField("TimeSpan");
        }
        set
        {
            SetField("TimeSpan", value);
        }
    }

    public Direction DirectionId
    {
        get
        {
            return (Direction)GetField("DirectionId");
        }
        set
        {
            SetField("DirectionId", value);
        }
    }

    [OneToOne(LocalKey = "StopId", ForeignKey = "StopId")]
    public Stop Stop
    {
        get
        {
            return (Stop)GetField("Stop");
        }
        set
        {
            SetField("Stop", value);
        }
    }

    [ManyToOne(LocalKey = "RouteId", ForeignKey = "RouteId")]
    public Route Route
    {
        get
        {
            return (Route)GetField("Route");
        }
        set
        {
            SetField("Route", value);
        }
    }
}

5 个答案:

答案 0 :(得分:1)

好的,在这种情况下,慢速渲染的来源似乎是RouteTime和Stop类之间的[OneToOne]连接。如果我绑定到链接类中存储的任何变量,则渲染需要很长时间。

修复

我在代码中添加了一个新的分部类,我需要显示结果。

public partial class StopTimes
{
    public int TimeSpan
    {
        get;
        set;
    }

    public string StopName
    {
        get;
        set;
    }
}

在Vici CoolStorage中使用ad-hoc查询,我已经提出了自己的查询来请求所需的数据和中提琴,一切都在那里,渲染时间不超过1秒。也许,在渲染过程中它会逐个请求带有SQLQuery的StopName字段?

不知道,但谢谢你的帮助:)

答案 1 :(得分:0)

DataBinding不应该是这个场景中的问题 - 我从来没有遇到任何问题。它确实与您的SQL DB有关。我有大约200个项目的列表,它在100毫秒的合理时间段内呈现。

它或者是您的SQL实现,或者您使用的是ViciCoolStorage错误。发布你的代码。

答案 2 :(得分:0)

您是否尝试过在LoadRouteStops()方法之外对列表进行过滤?也许在后面的代码而不是viewModel?在propertyChanging和propertyChanged通知之间做了很多工作。

答案 3 :(得分:0)

我很困惑:你如何在Windows Phone 7上使用本地SQLite?你的意思是支持WP8吗? 在任何情况下,请考虑使用LongListSelector(内置于WP8 - WP7中的Toolkit),因为当您有大量要呈现的项目时,它可以更好地进行虚拟化。

答案 4 :(得分:0)

问题主要是因为设置了很多绑定。尽量保持绑定最小化并尝试对后面的代码进行艰苦的工作。如;

&#34;将字符串列表逐个绑定到xaml&#34;
与 &#34;通过string.join方法只有一个绑定到文本块&#34;

我有

<TextBlock Text="{Binding Times[0]}"/>
<TextBlock Text="{Binding Times[1]}"/>
...
<TextBlock Text="{Binding Times[9]}"/>

并改为

<TextBlock Text="{Binding TimesToLongString}"/>

这解决了延迟问题。
用于在9秒内加载3件物品。
现在它在110毫秒内加载10件物品。

要检查加载列表所需的时间,您可以查看this post