1

在 silverlight 5 中:我构建了一个产品配置器,它还通过图像和矩形、椭圆、多边形结构的组合来构建绘图。

配置器工作正常,绘图也很好。当产品准备好时,它会被放置在画廊(列表框)中,并且会显示绘图的缩略图以及一些主要信息。到现在为止还挺好。

我还可以将库中的配置保存到光盘 (xml) 并稍后加载它们以继续并完成。这是问题所在。我将产品一一读给配置器,然后所有业务规则都到位,然后进行绘图。但在我将产品保存到画廊的那一刻,绘图的快照保持空白。

我确信所有步骤都很好,因为当我在将产品放入画廊之前中断时,绘图显示正确。然而,在画廊中,这幅画是空白的。所以我假设存在同步/定时/更新问题,但找不到强制重绘包含图像和绘图的网格的方法。

在 axml 中,列表框:

<ListBox x:Name="lbTiles" Loaded="lbTiles_Loaded" HorizontalAlignment="left" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Margin="2,2,0,0" Height="233" Width="892" d:LayoutOverrides="HorizontalAlignment" Background="Gainsboro" BorderThickness="2" BorderBrush="#FF606060">
<ListBox.ItemsPanel>
  <ItemsPanelTemplate>
      <StackPanel Orientation="Horizontal"/>
   </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  <ListBox.ItemTemplate>
      <DataTemplate >
         <StackPanel Orientation="Vertical" Background="Gainsboro">
            <Border BorderThickness="1" BorderBrush="#4F000000" >
              <Grid >
                 <Image Margin="0,0,0,0" Source="{Binding mImage, Mode=OneWay}" Stretch="Uniform" Height="110" ></Image>
              </Grid>
              </Border>
                <TextBox  TextAlignment="Center" IsReadOnly="True" Width="120" Height="23" Text="{Binding mName, Mode=OneWay}" IsHitTestVisible="False" IsTabStop="False" FontSize="11" />
                <TextBox  TextAlignment="Center" IsReadOnly="True" Width="120" Height="23" Text="{Binding mPrice, Mode=OneWay}" IsTabStop="False" IsHitTestVisible="False" FontSize="11" />
                 <TextBox  TextAlignment="Center" IsReadOnly="True" Width="120" Height="23" Text="{Binding mTotal, Mode=OneWay}" IsHitTestVisible="False" IsTabStop="False" FontSize="11" />
                  <TextBox  TextAlignment="Center" IsReadOnly="True" Width="120" Height="23" Text="{Binding mFreight, Mode=OneWay}" IsHitTestVisible="False" IsTabStop="False" FontSize="11" />
                  </StackPanel>
               </DataTemplate>
          </ListBox.ItemTemplate>
      </ListBox>

网格:

 <Grid Name="myDrawing" Background="Gainsboro" Height="437" Width="486">
    <Rectangle Name="clrFrame" Visibility="Collapsed" Margin="27,5,32,5" StrokeThickness="0" IsHitTestVisible="False" ></Rectangle>
       <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL1" VerticalAlignment="Top" Width="429" IsHitTestVisible="False" />
       <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL0" VerticalAlignment="Top" Width="429" IsHitTestVisible="False" />
        <Rectangle Name="clrWindow_1" Visibility="Collapsed" StrokeThickness="0" IsHitTestVisible="False" ></Rectangle>
         <Rectangle Name="clrWindow_2" Visibility="Collapsed" StrokeThickness="0" IsHitTestVisible="False" ></Rectangle>
         <Rectangle Name="clrWindow_3" Visibility="Collapsed" StrokeThickness="0" IsHitTestVisible="False" ></Rectangle>
         <Ellipse Name="clrWindow_4" Visibility="Collapsed" StrokeThickness="0" IsHitTestVisible="False" />
          <Polygon Name="clrWindow_5" Visibility="Collapsed" StrokeThickness="18" IsHitTestVisible="False"></Polygon>
          <Rectangle Name="clrDoor" Visibility="Collapsed" Margin="136,196,162,122" StrokeThickness="0" IsHitTestVisible="False" ></Rectangle>
          <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL2" VerticalAlignment="Top" Width="429" IsHitTestVisible="False"/>
          <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL3" VerticalAlignment="Top" Width="429" IsHitTestVisible="False" />
          <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL4" VerticalAlignment="Top" Width="429" IsHitTestVisible="False" />
          <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL5" VerticalAlignment="Top" Width="429" IsHitTestVisible="False" />
           <Image Height="429" HorizontalAlignment="Left" Margin="25.5,4,0,0" Name="imDrawingL6" VerticalAlignment="Top" Width="429" IsHitTestVisible="False" />
      </Grid>

C# 代码

for( int i = 0; i < count i++ )
{
xmlread al values to currentCasingComposition.....

writeToScreen(); // apply businessrules and do the drawing
myDrawing.InvalidateMeasure();
myDrawing.InvalidateArrange();
myDrawing.UpdateLayout();
// an exit here leaves me with the product in de confugurator and the drawing is fine
WriteableBitmap bmp = new WriteableBitmap(myDrawing, null); // capture drawing
currentCasingComposition.mImage = bmp;
currentCasingComposition.mImage.Invalidate();
currentCasingComposition.calculatePrice();
    ocCasingCompositions.Add(currentCasingComposition); to gallery via ObservableCollection
}

谁知道解决方法?

BR,吨

4

1 回答 1

1

UpdateLayout() 在 UI 线程上是异步的,因此您应该在调用块的其余部分之前等待它完成。不幸的是,我找不到关于如何在 UpdateLayout() 完成时获得通知的信息。所有引用它的示例都使用 DispatcherTimer 在继续之前等待一秒钟左右。所以我要做的是创建一个一次性调度程序计时器,将其间隔设置为 1000 毫秒(或更少,根据需要),然后在 Tick 事件上执行其余代码。

于 2013-04-06T13:53:57.940 回答