Using ResourceDictionaries, NullReferenceException
#3
I have checked the file you send and it is opened correctly in ViewerSvg.

After some additional investigation I have found a bug in ResourceDictionaryWriter.

It only works when you add svg files to ResourceDictionary with AddGeometryFile or AddGeometryStream methods (this means that svg file will be read as Geometry and than added to the ResourceDictionary). But ResourceDictionaryWriter does not work if you use AddFile or AddStream method (read svg file as Shapes and add them to resource dictionary). Actually the last two methods work if they are called after calling AddGeometryFile or AddGeometryStream methods.

I have marked this as P1 bug and will fix it in the next release. Currently I am finishing a new version of ReaderWmf. Than I will do some improvements on ReaderSvg and fix this also.

Until than you can use the following nasty workaround to make your code work - before first use of AddFile or AddStream method write the following (resourceDictionaryWriter is an instance of ResourceDictionaryWriter):

Code:
_resourceDictionaryWriter.AddGeometryStream(new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes("<svg></svg>")), new GeometrySettings()); ;

This will initialize a private filed that would otherwise throw NullReferenceException. After getting xaml you will also have an empty DrawingImage as the first element in ResourceDictionary - but it can be simply deleted by hand.


With the provided workaround it is possible to use AddFile or AddStream method. However I would like to advice you to use AddGeometryFile or AddGeometryStream methods instead.

This would give you the following advantages:
- your objects would be represented as Geometry elements - therefore would have better performance and use lower memory.
- ReaderSvg can additionally optimize the read geometry. Already with using basic optimization the ResourceDictionaryWriter would create separate ResourceDictionary entries for brushes and pens that are commonly used. For example black pen can than be used for all the objects included in ResourceDictionary (also from more than one svg file).

The following screenshow shows this:
[Image: http://www.wpf-graphics.com/images/SvgRe...Writer.png]

A drawback of using Geometry elements is that they provide less layout, focus and other features.


The code you used can be simply changed into code that will create ResourceDictionary with Geometry elements and with combined brushes and pens:
Code:
var geometryDictionary = new ResourceDictionaryWriter();
using (var stream = file.OpenRead())
    shapeDictionary.AddGeometryStream(stream, Ab2d.Common.ReaderSvg.GeometrySettings.BasicOptimization);

A good sample on how to create ResourceDictionary with Geometry elements is also part of ReaderSvg package - a ResourceDictionary writer application will full source code.
  


Messages In This Thread
RE: Using ResourceDictionaries, NullReferenceException - by abenedik - 07-27-2010, 11:17 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)