所以,我对我的microsoft visual studio做了一些更新。之前,我的UWP flipview工作得很好。基本上,我的Flipview从本地图片库读取图像。现在如果我运行我的UWP,这个异常就会出现!
System.DivideByZeroException
HResult=0x80020012
Message=Attempted to divide by zero.
Source=TMD_Latest
StackTrace:
at TMD_Latest.Views.MainPage.ChangeImage(Object sender, Object o) in C:\Users\alish\source\repos\TMD_Latest\TMD_Latest\Views\MainPage.xaml.cs:line 97
private void ChangeImage(object sender, object o)
{
//Get the number of items in the flip view
var totalItems = TheFlipView.Items.Count;
//Figure out the new item's index (the current index plus one, if the next item would be out of range, go back to zero)
//This line below is the exception!
var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems;
//Set the displayed item's index on the flip view
TheFlipView.SelectedIndex = newItemIndex;
}我的xaml:
<Grid VariableSizedWrapGrid.ColumnSpan="5"
VariableSizedWrapGrid.RowSpan="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Padding="0 30 20 20"
Margin="200,-100,0,10"
Background="Transparent">
<x21:Grid.RowDefinitions>
<x21:RowDefinition Height="405*"/>
<x21:RowDefinition Height="21*"/>
<x21:RowDefinition Height="425*"/>
</x21:Grid.RowDefinitions>
<FlipView x:Name="TheFlipView"
SelectionChanged="DisplayedItemChanged" Margin="-235,205.6,30,-1221.6" x21:Grid.Row="2" >
<FlipView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,10" >
<Image HorizontalAlignment="Center" VerticalAlignment="Stretch" Source="{Binding}"
Stretch="Fill" Margin="0,-200,0,0" />
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>请帮助我:(
发布于 2019-02-11 02:09:22
正如@Abestrad提到的,这是因为余数运算符的右侧为零。如前所述,here
x % y的结果是x - (x / y) * y产生的价值。如果y_System.DivideByZeroException为零,则抛出异常。
在您的情况下,解决此问题的一种方法是将余数运算符包装在if条件中:
if (totalItems > 0)
{
var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems;
//Set the displayed item's index on the flip view
TheFlipView.SelectedIndex = newItemIndex;
}https://stackoverflow.com/questions/54619269
复制相似问题