UWP Windows 10手机中的ScrollViewer无法滚动

3

非常抱歉我的措辞很简单,但我是一个学徒,对Windows 10和UWP开发还很新。

我目前为我们的应用程序创建一个手册页面,该页面可以从许多不同的页面调用,并且根据哪个页面调用它来调用相应的手册。

我尝试使用PDF,但发现PDF的处理非常缓慢和古怪,因此我将所有手册页面保存为.PNG文件。

我在XAML中有一个包含文件链接的字典项,在我的C#代码中,我决定要显示哪一页和哪个手册。

有些页面可能只有一页,而其他页面可能有6页或更多。

我计划暂时将图片包含在启用缩放的ScrollView中,这个方法完美运行,除了一个主要问题。

我正在尝试创建一个带有6个图像(垂直对齐)的ScrollViewer。由于图像大小,您只能在屏幕上看到2个图像(因此需要ScrollViewer)。我实际上唯一能够滚动下去查看其余图像的方法是放大,然后滚动。您需要多次这样做才能到底部,但是当您缩小以查看完整图像时,ScrollViewer会自动滚动回顶部。

这是我的Xaml代码:

<ScrollViewer x:Name="MyScrollViewer" ZoomMode="Enabled" MaxZoomFactor="5" MinZoomFactor="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible"  VerticalScrollMode="Enabled">
    <StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
        <Image Name="HelpManual" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual2" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual3" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual4" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual5" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual6" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
    </StackPanel>
</ScrollViewer>

所有图像均设置为BlankPage Resource(即空白),因为我将在另一个页面中使用此内容,该页面上的图像较少。我只需从代码后台更改图像源即可。

页面资源:

<Page.Resources>
    <BitmapImage x:Key="BlankPage" UriSource="" />
    <BitmapImage x:Key="LightingPage" UriSource="Assets\AppPages\HelpManuals\LightingPageManual.png" />
    <BitmapImage x:Key="PowerPage" UriSource="Assets\AppPages\HelpManuals\PowerPageManual.png" />
    <BitmapImage x:Key="WaterPage" UriSource="Assets\AppPages\HelpManuals\WaterPageManual.png" />
    <BitmapImage x:Key="HeatingPage" UriSource="Assets\AppPages\HelpManuals\HeatingPageManual.png" />
    <BitmapImage x:Key="RemotePage" UriSource="Assets\AppPages\HelpManuals\RemotePageManual.png" />
    <BitmapImage x:Key="BluetoothPage" UriSource="Assets\AppPages\HelpManuals\BluetoothPageManual.png" />
</Page.Resources>

图像来源存储在此文件顶部的页面资源中,并通过后台代码进行更改,如下所示:
void Page_LoadComplete(object sender, RoutedEventArgs e)
{
    var lastPage = Frame.BackStack.Last().SourcePageType;

    if (lastPage.Name == "PowerPage")
    {
        HelpManual.Source = (ImageSource)Resources["PowerPage"];
    }
    else if (lastPage.Name == "MainPage")
    {
        HelpManual.Source = (ImageSource) Resources["LightingPage"];
        HelpManual2.Source = (ImageSource) Resources["PowerPage"];
        HelpManual3.Source = (ImageSource)Resources["WaterPage"];
        HelpManual4.Source = (ImageSource)Resources["HeatingPage"];
        HelpManual5.Source = (ImageSource)Resources["RemotePage"];
        HelpManual6.Source = (ImageSource)Resources["BluetoothPage"];
    }
}

非常感谢您的帮助。感谢@LovetoCode提出的建议。我已经将我的ScrollViewer更改为Grid,而不是StackPanel,但问题仍然存在。
我在Grid中创建了6行(每个项目一个),并将每个项目添加到它们各自的行中。但是,仍然很难向下滚动。唯一真正的“滚动”方式是用一个手指放在屏幕上,然后用另一个手指滚动,这类似于缩放。一旦您将手指从屏幕上拿开,ScrollViewer就会立即回到顶部。当我从新网格中删除行时,只显示了一个图像,其他图像都看不到了。
这是我的新ScrollViewer:
<ScrollViewer x:Name="MyScrollViewer" ZoomMode="Enabled" MaxZoomFactor="5" MinZoomFactor="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible"  VerticalScrollMode="Enabled" >
    <Grid Name="MyScrollViewGrid" >
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Image Name="HelpManual" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual2" Grid.Row="1" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual3" Grid.Row="2" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual4" Grid.Row="3" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual5" Grid.Row="4" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
        <Image Name="HelpManual6" Grid.Row="5" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
    </Grid>                   
</ScrollViewer>

由于StackPanel的原因,您无法滚动。 - Archana
@LovetoCode 我修改了代码,但没有成功,我已经在问题底部添加了修改内容和说明。 - James Tordoff
如果我找到解决方案,我会回复你。与此同时,你可以尝试使用ListView,每个ListViewItem包含一张图片。 - Archana
抱歉,我尝试使用StackPanel本身。我能够滚动页面。 - Archana
谢谢!ListView与ListViewItmes完美地配合使用。感谢您的耐心,非常感激! - James Tordoff
显示剩余7条评论
2个回答

3

尝试使用ListView代替

<ListView ScrollViewer.ZoomMode="Enabled" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListViewItem>
                <Image Name="HelpManual" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual1" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual2" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual3" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
        </ListView>

这个Came恰好在我发布我的回答之后!我上面的答案全都要归功于@LovetoCode。这个方法效果太棒了,使用它的表现比在Windows 10 UWP中使用PDF在webview内要快得多,而且很容易出问题。 - James Tordoff

1

已解决!

不再使用StackPanel来放置ScrollViewer中的所有图像,而是使用ListView,每个正在显示的项都有一个ListViewItem。 C#代码和Page.Resources仍然与原来完全相同。 现在的ScrollViewer是带有ListView而不是StackPanel的不同。 这也意味着我不使用多行,而只使用一个用于ListView。

感谢@LovetoCode!

以下是修正后的ScrollViewer的新代码:

      <ScrollViewer x:Name="MyScrollViewer" ZoomMode="Enabled" MaxZoomFactor="5" MinZoomFactor="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible"  VerticalScrollMode="Enabled" >
        <ListView>
            <ListViewItem>
                <Image Name="HelpManual" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual2" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual3" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual4" Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual5"  Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
            <ListViewItem>
                <Image Name="HelpManual6"  Source="{StaticResource BlankPage}" Width="{Binding Path=ViewportWidth, ElementName=MyScrollViewer}"/>
            </ListViewItem>
        </ListView>
    </ScrollViewer>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接