Using ResourceDictionaries, NullReferenceException
#5
The NamedObjectsSource property works correctly (getting names from Id property). If you check the Name property of children of read Viewbox, you will see that the name is correctly set.

The problem is that the GetXaml method uses only NamedObjects dictionary for getting the object name. This has been already improved in the development version and will also come with the next release.

Until than you need to explicitly set the NamedObjects for the GetXaml method. For example:

Code:
Ab2d.ReaderSvg readerSvg = new ReaderSvg();
readerSvg.NamedObjectsSource = ReaderSvg.NamedObjectsSourceType.Id;
readerSvg.Read(fileName);

WpfXamlWriterSettings wpfXamlWriterSettings = new WpfXamlWriterSettings();
wpfXamlWriterSettings.NamedObjects = readerSvg.NamedObjects;

string xaml = readerSvg.GetXaml(wpfXamlWriterSettings);


If I understand you correctly you are sending clients ResourceDictionaries in xaml format. Why don't you make a project from a ResourceDictionaries, compile it and send the client a dll.

It will be much smaller and what is more data from it will be read much faster. Also there the number of decimals would not matter.

If dll is not ok for you than I have done some additional coding and prepared a source code for new ShapesResourceDictionaryWriter that works only with shapes. You can use the source for your projects:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;
using Ab2d.Common.ReaderSvg;

namespace Ab2d.Utility.ReaderSvg
{
    /// <summary>
    /// ResourceDictionaryWriter class is a helper class that can be used to build ResourceDictionaries from multiple input files.
    /// </summary>
    public class ShapesResourceDictionaryWriter
    {
        private const string DefaultXamlNamespace = "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:PresentationOptions=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation/options\"";

        private Ab2d.ReaderSvg _myReader;

        private List<string> _readXamls;

        private int _resourceIndex;

        private WpfXamlWriterSettings _wpfXamlWriterSettings;

        /// <summary>
        /// Constructor.
        /// </summary>
        public ShapesResourceDictionaryWriter()
        {
            _myReader = new Ab2d.ReaderSvg();

            Reset();

            _wpfXamlWriterSettings = new WpfXamlWriterSettings();

            _wpfXamlWriterSettings.NamedObjects = null;
            _wpfXamlWriterSettings.RootObject = "";
            _wpfXamlWriterSettings.XamlNamespace = "";
            _wpfXamlWriterSettings.StartComment = "";
            _wpfXamlWriterSettings.NumberFormatString = "0.0";
        }

        private void AddReadXamlToDictionary()
        {
            string xaml;
            int pos1, pos2;

            xaml = _myReader.GetXaml(_wpfXamlWriterSettings);
            
            pos1 = xaml.IndexOf("<Canvas") + "<Canvas".Length;
            pos2 = xaml.LastIndexOf("</Canvas>") + "</Canvas>".Length;

            // Strip off comment and Viewbox and add x:Key
            xaml = string.Format("    <Canvas x:Key=\"Canvas_{0}\"", _resourceIndex) + xaml.Substring(pos1, pos2 - pos1);

            _resourceIndex++;

            _readXamls.Add(xaml);
        }

        /// <summary>
        /// Resets the collected svg files so a new ResourceDictionary can be created.
        /// </summary>
        public void Reset()
        {
            _readXamls = new List<string>();
            _resourceIndex = 0;
        }

        /// <summary>
        /// Adds a svg or svgz file to the ResourceDictionary as WPF Shapes.
        /// </summary>
        /// <param name="fileName">fileName</param>
        /// <returns>read svg or svgz file as Viewbox</returns>
        public Viewbox AddFile(string fileName)
        {
            Viewbox viewbox;

            if (string.IsNullOrEmpty(fileName))
                return null;

            viewbox = _myReader.Read(fileName);
            AddReadXamlToDictionary();

            return viewbox;
        }

        /// <summary>
        /// Adds a svg or svgz from stream to the ResourceDictionary as WPF Shapes.
        /// </summary>
        /// <param name="stream">stream</param>
        /// <returns>read svg or svgz file as Viewbox</returns>
        public Viewbox AddStream(Stream stream)
        {
            Viewbox viewbox;

            viewbox = _myReader.Read(stream);
            AddReadXamlToDictionary();

            return viewbox;
        }

        /// <summary>
        /// Gets the xaml string of the ResourceDictionary with all added svg objects as resources.
        /// </summary>
        /// <returns>xaml</returns>
        public string GetXaml()
        {
            StringBuilder sb;

            sb = new StringBuilder();
            sb.Append("<!-- Converted from svg or svgz with Ab2d.ReaderSvg. See www.wpf-graphics.com for more WPF tools.-->").AppendLine();
            sb.Append("<ResourceDictionary ").Append(DefaultXamlNamespace).Append(">").AppendLine();

            foreach (string oneInnerXaml in _readXamls)
                sb.Append(oneInnerXaml).AppendLine();

            sb.Append("</ResourceDictionary>");

            return sb.ToString();
        }
    }
}
  


Messages In This Thread
RE: Using ResourceDictionaries, NullReferenceException - by abenedik - 07-28-2010, 10:34 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)