有没有办法缩短这个if()
陈述?为了避免string.equals()
某种方式重复?
if (extension.equals("jpg") || extension.equals("JPG") || extension.equals("png") || extension.equals("PNG") || extension.equals("bmp") || extension.equals("BMP") || extension.equals("jpeg") || extension.equals("JPEG"))
{
tmp.setIcon(new ImageIcon(getClass().getResource("/menage/Resources/imageIco.png")));
}
Run Code Online (Sandbox Code Playgroud)
看起来像这样的东西:
if (extension.equals(("jpg")||("JPG")||("png")||("PNG")||("bmp")||("BMP")||("jpeg")||("JPEG")))
{
tmp.setIcon(new ImageIcon(getClass().getResource("/menage/Resources/imageIco.png"));)
}
Run Code Online (Sandbox Code Playgroud)
我知道这个问题看起来很奇怪,但是if()
如此长的条件列表不清楚,需要大量的写作.
我创建了一个需要绘制简单矩形(1x1 - 3x3大小)的应用程序取决于以20,40或甚至60 FPS速率存储在相应大小(900x900 - 300x300大小)的数组中的一些变量.
这是我的绘图方法:
protected void drawCurrentState()
{
for (int i=0; i<gameLogic.size; i++)
{
for (int j=0; j<gameLogic.size; j++)
{
if (gameLogic.previousStepTable == null || gameLogic.previousStepTable[i][j] != gameLogic.gameTable[i][j])
{
if (gameLogic.gameTable[i][j].state)
graphicsContext.setFill(gameLogic.gameTable[i][j].color);
else
graphicsContext.setFill(Color.WHITE);
graphicsContext.fillRect(leftMargin + (j * (cellSize + cellMargin)),
topMargin + (i * (cellSize + cellMargin)), cellSize, cellSize);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,只有在需要时(当状态从上一步开始改变时)才执行绘图.所有其他操作(gameLogic等中的计算变量状态)几乎不需要时间,也不会影响性能.
JavaFX执行此操作非常慢!在5-10秒内绘制10次迭代(应该在20FPS速率下以0.5秒绘制),这在这种情况下显然是不可接受的.
有没有办法大规模地将绘图操作加速到预期的性能水平(例如,每秒40或60次迭代)?
那么在这种情况下绘画表现如此糟糕的原因是什么?
我正在努力从 DataTemplate/ContextMenu 控件绑定到父 DataContext。我已经尝试过此处和此处提到的解决方案,但没有运气。我无法绑定到属性或命令 - 不会引发异常,但不会调用命令并且属性设置为 null。
这是我的代码示例(尽可能简化):
<Metro:MetroWindow ...>
<Window.DataContext>
<local:MyViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<StackPanel ... />
<Grid Grid.Column="1">
<Border ... />
<ListBox x:Name="FileList" ItemsSource="{Binding AddedFiles}" Margin="5,5,5,5" SelectionMode="Multiple" SelectionChanged="ItemsSelectionChanged">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel MaxWidth="700" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:FileListItem}">
<Grid Margin="5" Width="110" ToolTip="{Binding Path=TooltipInfo}" MouseDown="FileItemClick" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding PlacementTarget.Tag.test,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" />
<MenuItem Header="Test command" Command="{Binding PlacementTarget.Tag.CloseCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/> …
Run Code Online (Sandbox Code Playgroud) java ×2
c# ×1
data-binding ×1
drawing ×1
if-statement ×1
javafx ×1
performance ×1
wpf ×1
xaml ×1