XamlToys Doesn't work on framework 4.0???

Apr 26, 2012 at 9:10 AM
Edited Apr 27, 2012 at 10:34 AM

Hi,

Thanks for building such a useful utility.

I wanted to use XamlToys in my application which is built on .New Framework 4.

When I call  BCDev.XamlToys.Utility.GetDrawingFromXaml(XamlReader.Load(xpsPageStream)); it throws  exception and says I/O error occured in mscorelib.

When I dig deeper in XamlToys code, I came to know that it throws that exception in RealizeFrameworkElement(), while doing Measure on framework element.

I am trying to use XamlToys in Framework 4.0, it works perfect in 3.5, but it fails miserably in 4.0.

Can you please help me solve my problem?

Please find the Stacktrace below:

Which hints towards the problem in "System.Windows.Media.GlyphTypeface.Initialize()"

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=PresentationCore
  StackTrace:
       at MS.Internal.FontCache.FontFaceLayoutInfo.IntMap.TryGetValue(Int32 key, UInt16& value)
       at MS.Internal.FontCache.FontFaceLayoutInfo..ctor(Font font)
       at System.Windows.Media.GlyphTypeface.Initialize(Uri typefaceSource, StyleSimulations styleSimulations)
       at System.Windows.Media.GlyphTypeface..ctor(Uri typefaceSource, StyleSimulations styleSimulations)
       at System.Windows.Documents.Glyphs.ParseGlyphRunProperties()
       at System.Windows.Documents.Glyphs.ComputeMeasurementGlyphRunAndOrigin()
       at System.Windows.Documents.Glyphs.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Documents.FixedPage.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at BCDev.XamlToys.Utility.RealizeFrameworkElement(FrameworkElement fe) in E:\Practice\XamlToys_1.0_src\XamlToys\XamlToys.cs:line 41
       at BCDev.XamlToys.Utility.GetDrawingFromXaml(Object xaml) in E:\Practice\XamlToys_1.0_src\XamlToys\XamlToys.cs:line 52
       at SampleForXamlToys.MainWindow.CopyXAMLStreamToWmfClipBoard(Stream drawingStream, Window clipboardOwnerWindow) in E:\Practice\ForSnapshot\SampleForXamlToys\SampleForXamlToys\MainWindow.xaml.cs:line 122
       at SampleForXamlToys.MainWindow.CreateSnapshotMetafile(Visual visual, Window clipboardOwnerWindow) in E:\Practice\ForSnapshot\SampleForXamlToys\SampleForXamlToys\MainWindow.xaml.cs:line 106
       at SampleForXamlToys.MainWindow.BtnCopyToEmfClick(Object sender, RoutedEventArgs e) in E:\Practice\ForSnapshot\SampleForXamlToys\SampleForXamlToys\MainWindow.xaml.cs:line 35
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at SampleForXamlToys.App.Main() in E:\Practice\ForSnapshot\SampleForXamlToys\SampleForXamlToys\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

May 10, 2012 at 3:52 PM
Edited May 11, 2012 at 2:24 PM

This occures when reading xps stream.

Framework 4.0 wants to have the temporary fonts in files with extension TTF and in directories with the same name as the font, Framework 3.5 accepted ODTTF.

Just using extension TTF and puting the file in a folder with the same name is enough, to get everything working.

I don't know your code, but in my code I have a minor problem, not being able to delete the TTF-files after usage, someone is holding a reference to these files and I don't know who.

Nov 6, 2012 at 6:14 PM

Great thanks.

I was only saving the font in temp folder and it was working the first shot. I need to close the app to convert another time. By adding the folder with the name of the font solved my problem.

Thanks,

Eric