-2

NotifyCollectionChangedEventHandler command does not work with background worker.Event is as :

void MainWindowsViewModel_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    StrokesEllipse = ((StrokeCollection)sender);
    using (var memoryStream = new MemoryStream())
    {
        StrokesEllipse.Save(memoryStream);
        //convert memory stream to  array
        EllipseDrawing = memoryStream.ToArray();
        //save the above array to say - database
        }
    }

And we declared event on constructor as below

_strokesEllipse = new StrokeCollection();
(_strokesEllipse as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(MainWindowsViewModel_CollectionChanged);

And we are binding stoke collection on the background worker completed event. as Below

string s = GetMechanicSignature();
if (s != "")
{
    EllipseDrawing = Convert.FromBase64String(s);
}
if (EllipseDrawing != null)
{
    try
    {
        using (var memoryStream = new MemoryStream(EllipseDrawing))
        {
            _strokesEllipse = new StrokeCollection(memoryStream);
        }
    }
    catch (Exception)
    {

}

inkcanvas control does not showing loaded data. why? When we try without background worker then inkcanvas control loading the data very well? inkcanvas xml is as below

<InkCanvas x:Name="inkCanVas" Grid.Row="0" IsEnabled="{Binding VCRSignatureModel.IsEnable,Mode=TwoWay}" Background="White"  Width="700" Height="90" Margin="40,0,0,0" Strokes="{Binding StrokesEllipse,Mode=TwoWay}">
    <InkCanvas.DefaultDrawingAttributes>
        <DrawingAttributes Color = "Black" Width = "6" />
    </InkCanvas.DefaultDrawingAttributes>
</InkCanvas>
naina
  • 63
  • 1
  • 9

1 Answers1

1

You are not modifying the collection, you are replacing it. As your background work completed event should be firing in the UI thread it is not a threading issue.

The quickest way to fix this would be to add the following line after the line _strokesEllipse = new StrokeCollection(memoryStream); in your worker completed code.

MainWindowsViewModel_CollectionChanged(
   _strokesEllipse,
   new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace);

Alternatively you could change the code to read:

try
{
    using (var memoryStream = new MemoryStream(EllipseDrawing))
    {
        var newCollection = new StrokeCollection(memoryStream);
        _strokesEllipse.Clear();
        _strokesEllipse.Add(newCollection);
    }
}
catch (Exception)
{
Bob Vale
  • 18,094
  • 1
  • 42
  • 49