private void rect_ManipulationStarting(object sender, ManipulationDeltaRoutedEventArgs e)
{
startingColumn = Grid.GetColumn(e.OriginalSource as Windows.UI.Xaml.Shapes.Rectangle);
gLValue = headerGrid.ColumnDefinitions[startingColumn].Width.Value + e.Delta.Translation.X;
if (gLValue < 5)
return;
else
{
headerGrid.ColumnDefinitions[startingColumn].Width = new GridLength(gLValue);
cycleThroughColumns(libraryGrid, startingColumn, gLValue);
cycleThroughColumns(playlistGrid, startingColumn, gLValue);
}
}
private void cycleThroughColumns(Grid anyGrid, int anyStartingColumn, double anyGLValue)
{
if (anyGrid.Children.Count > 0)
{
if ((anyGrid.Children[0] as Grid).ColumnDefinitions.Count > 0)
{
for (int index = 0; index < anyGrid.Children.Count; index++)
{
(anyGrid.Children[index] as Grid).ColumnDefinitions[anyStartingColumn].Width = new GridLength(anyGLValue);
}
}
}
}
In the header there is a rectangle on each column divider, that when moved calls the event handler. Within libraryGrid and playlistGrid are grids for each row. (In each of these grids is a textblock). It's just too slow when resizing 884 columns and I was wondering if there was a way to optimize the code to speed it up. Only about 20 rows are visible to the user at one time.
Here are changes I made using a poor-man's virtualization technique, I took the height of the scrollviewer and divided by the height of the items to get the number of items, added 2 for good measure, and now I loop through about 10-15 items first (the visible ones) before I loop through the second set, both are asynchronous, although it still has some lag it's much, much more responsive. Any further optimization ideas are also appreciated.
private void rect_ManipulationStarting(object sender, ManipulationDeltaRoutedEventArgs e)
{
startingColumn = Grid.GetColumn(e.OriginalSource as Windows.UI.Xaml.Shapes.Rectangle);
gLValue = headerGrid.ColumnDefinitions[startingColumn].Width.Value + e.Delta.Translation.X;
if (gLValue < 5)
return;
else
{
headerGrid.ColumnDefinitions[startingColumn].Width = new GridLength(gLValue);
cycleThroughColumns(libraryGrid, startingColumn, gLValue);
cycleThroughColumns(playlistGrid, startingColumn, gLValue);
}
}
private void cycleThroughColumnsAsync(Grid anyGrid, int anyStartingColumn, double anyGLValue, int anyVertStart, int anyVertEnd)
{
if (anyGrid.Children.Count > 0)
{
if ((anyGrid.Children[0] as Grid).ColumnDefinitions.Count > 0)
{
for (int index = anyVertStart; index < anyVertEnd; index++)
{
if (anyGrid.Children[index] as Grid != null)
{
(anyGrid.Children[index] as Grid).ColumnDefinitions[anyStartingColumn].Width = new GridLength(anyGLValue);
}
else
{
break;
}
}
}
}
}
async private void cycleThroughColumns(Grid anyGrid, int anyStartingColumn, double anyGLValue)
{
int vertStart = getVertStart(anyGrid);
int vertEnd = getVertEnd(vertStart, anyGrid);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => cycleThroughColumnsAsync(anyGrid, anyStartingColumn, anyGLValue, vertStart, vertEnd));
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => cycleThroughColumnsLeftOverAsync(anyGrid, anyStartingColumn, anyGLValue, vertStart, vertEnd));
}
private void cycleThroughColumnsLeftOverAsync(Grid anyGrid, int anyStartingColumn, double anyGLValue, int anyVertStart, int anyVertEnd){
for (int index = 0; index < anyVertStart; index++)
{
if (anyGrid.Children[index] as Grid != null)
{
(anyGrid.Children[index] as Grid).ColumnDefinitions[anyStartingColumn].Width = new GridLength(anyGLValue);
}
else
{
break;
}
}
for (int index = anyVertEnd; index < anyGrid.Children.Count; index++)
{
if (anyGrid.Children[index] as Grid != null)
{
(anyGrid.Children[index] as Grid).ColumnDefinitions[anyStartingColumn].Width = new GridLength(anyGLValue);
}
else
{
break;
}
}
}
private int getVertStart(Grid anyGrid)
{
return (int)(anyGrid.Parent as ScrollViewer).VerticalOffset / 30;
}
private int getVertEnd(int anyVertStart, Grid anyGrid)
{
double numberOfRowsInView = (anyGrid.Parent as ScrollViewer).RenderSize.Height / 30;
return (int)(anyVertStart + numberOfRowsInView) + 2;
}