wpf - How to reference types in executing assembly namespace when parsing with XamlReader.Load() -


this xaml in @"data/example.xml". it's text file ("content", "copy always") parse using xamlreader.load() @ runtime, because design decision:

<datatemplate     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="clr-namespace:testproject"     >     <datatemplate.resources>         <local:typenameconverter x:key="typename" />     </datatemplate.resources>     <label          content="{binding converter={staticresource typename}}"          /> </datatemplate> 

it's fine without value converter , local namespace attribute. those, when parsed xamlreader.load(), throws exception:

'cannot create unknown type '{clr-namespace:testproject}typenameconverter'.' line number '7' , line position '10'.

my question is:

is there way make assembly reference work executing assembly in scenario?

some more detail things are, how think ruled out errors on part, , alternate approaches work:

typenameconverter arbitrary value converter defined in executing assembly -- assembly calling xamlreader.load(). instantiates you'd expect in mainwindow.xaml.

namespace testproject {     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();              using (xmltextreader reader = new xmltextreader(@"data/example.xml"))             {                 //  demonstrating exception here.                 xamlreader.load(reader);             }         }      }      public class typenameconverter : ivalueconverter     {         public object convert(object value,              type targettype, object parameter, cultureinfo culture)         {             if (value == null)                 return "";              return value.gettype().name;         }          public object convertback(object value,              type targettype, object parameter, cultureinfo culture)         {             throw new notimplementedexception();         }     } } 

the following attempts @ defining namespace failed:

    xmlns:local="clr-namespace:testproject;assembly=."     xmlns:local="clr-namespace:testproject;assembly="     xmlns:local="clr-namespace:testproject;assembly=testproject.exe" 

the second 1 apparently documented @ point , never worked, didn't cost me try it.

what succeeded creating second assembly contained converter:

<datatemplate     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:ext="clr-namespace:xamlextensions;assembly=myotherassembly"     >     <datatemplate.resources>         <ext:typenameconverter x:key="typename" />     </datatemplate.resources>     <label          content="{binding converter={staticresource typename}}"          /> </datatemplate> 

obviously go in window.resources well. that's the user sparked question going end doing.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -