我在Windows Phone 8.1中创建了一个类似Gallary的应用程序。 我已经实现了flipview来显示图像现在我想在增量加载中显示图像,用户也可以通过缩放缩放来缩放特定图像。我已经实现了一些代码,但没有以正确的方式我无法缩放图像peroperper并且我也无法更改图像,而通过滑动应用程序更改屏幕上的图像崩溃。请帮我如何做我的功能。我的Xaml代码如下所示:
<Grid Grid.Row="1" Margin="0,0,0,0" x:Name="rootGrid" Loaded="rootGrid_Loaded" >
<FlipView x:Name="flipview" SelectionChanged="FlipViewSelectionChanged" Loaded="flipViewLoaded" >
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<ScrollViewer x:Name="scrollViewer" MinZoomFactor="1" ZoomMode="Enabled" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
<Image Source="{Binding Image}" Stretch="Uniform" HorizontalAlignment="Center" Height="{Binding Mode=TwoWay, ElementName=flipview, Path=Height}" Width="{Binding Mode=TwoWay, ElementName=flipview, Path=Width}" />
</ScrollViewer>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
和C#这样:
int offset = 0;
bool incall = false, endoflist = false;
private void rootGrid_Loaded(object sender, RoutedEventArgs e)
{
flipview.ItemsSource = sourceList;
fetchPhotos(0);
}
private void flipViewLoaded(object sender, RoutedEventArgs e)
{
ScrollViewer viewer = GetScrollViewer(flipview);
viewer.ViewChanged += mainView_ViewChanged;
}
public static ScrollViewer GetScrollViewer(DependencyObject depObj)
{
if (depObj is ScrollViewer) return depObj as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = GetScrollViewer(child);
if (result != null) return result;
}
return null;
}
private void mainView_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
ScrollViewer view = (ScrollViewer)sender;
double progress = view.VerticalOffset / view.ScrollableHeight;
System.Diagnostics.Debug.WriteLine(progress);
if (progress > 0.7 & !incall && !endoflist)
{
incall = true;
fetchPhotos(++offset);
}
}
private async void fetchPhotos(int offset)
{
int start = offset * 8;
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder photosFolder = await localFolder.GetFolderAsync("Photos");
StorageFolder Folder = await photosFolder.GetFolderAsync(folderName);
var allphotos = await Folder.GetFilesAsync();
for (int i = start; i < start + 8; i++)
{
if (i < allphotos.Count)
{
foreach (photos.PhotosClass abc in PassedData.passedSourceList)
{
string filename = abc.Name;
StorageFile file = await Folder.GetFileAsync(filename);
BitmapImage image = new BitmapImage();
var randomAccessStream = await file.OpenReadAsync();
image.SetSource(randomAccessStream);
sourceList.Add(new photos.PhotosClass { Name = filename, Image = image });
}
}
else
{
endoflist = true;
break;
}
}
incall = false;
}
private void FlipViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is FlipView)
{
FlipView item = (FlipView)sender;
var flipViewItem = ((FlipView)sender).ItemContainerGenerator.ContainerFromIndex(((FlipView)sender).SelectedIndex);
var scrollViewItem = FindFirstElementInVisualTree<ScrollViewer>(flipViewItem);
if (scrollViewItem is ScrollViewer)
{
ScrollViewer scroll = (ScrollViewer)scrollViewItem;
scroll.ScrollToHorizontalOffset(0);
scroll.ScrollToVerticalOffset(0);
scroll.ZoomToFactor(1.0f);
}
}
}
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
if (parentElement != null)
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
return (T)child;
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
{
return result;
}
}
}
}
return null;
}