非常抱歉我的措辞很简单,但我是一个学徒,对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>