c# - XAML ObjectDataProvider, ObjectType Error -


i'm trying instantiate object (that created in c#) in xaml using objectdataprovider. unfortunately, i'm receiving following error: "the type reference cannot find public type named 'typename'". have .cs file of same name the typename.

here's xaml:

<window x:class="projectname.projectfile"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"         xmlns:local="clr-namespace:projectname"         title="projectfile" height="500" width="500">     <window.resources>         <resourcedictionary>             <!-- line 9 1 right below one. -->             <objectdataprovider x:key="name1" objecttype="{x:type typename}"/>              <objectdataprovider x:key="name2" objectinstance="{staticresource typename}" methodname="methodname"/>         </resourcedictionary>     </window.resources>     <grid height="375">         <dockpanel datacontext="{binding source={staticresource typename}}" width="440" margin="10,20,191,35">             <dg:datagrid name="dg" itemssource="{binding}"/>         </dockpanel>         <dockpanel width="85" height="25" margin="0,350,0,0">             <frame name="frame"/>             <button content="see posts info" click="button_click"/>         </dockpanel>     </grid> </window> 

here's c# file typename:

using system; using system.collections.generic; using system.linq; using system.text; using system.data;  namespace projectname {     class typename     {         private accesdbdataset data_set;         private accesdbdatasettableadapters.tbltypenametableadapter typenameadapter;          public typename()         {             data_set = new accesdbdataset();             datatable tbltypename = data_set.tables[1];             typenameadapter = new accesdbdatasettableadapters.typenameadaptertableadapter();             typenameadapter.fill(data_set.tbltypenameadapter);         }          public dataview methodname()         {             return data_set.tbltypenameadapter.defaultview;         }     } } 

so, why typename unrecognized in line 9 of xaml? formatting incorrectly? tried setting

<objectdataprovider x:key="name1" objecttype="{x:type local:typename}"/>  

but caused same bug.

thanks help!

i see couple of errors. need tell xaml processor qualified type name. since doesn't understand c# namespaces, have use clr-namespace declaration (you have one, aren't using it). second, staticresource should pointing x:key attribute on first objectdataprovider. should work:

<objectdataprovider x:key="name1" objecttype="{x:type local:typename}"/> <objectdataprovider x:key="name2" objectinstance="{staticresource name1}" methodname="methodname"/> 

note binding has incorrect staticresource well. since i'm not sure objectdataprovider want use it, didn't pick one. need use either name1 or name2 key staticresource.

edit using clr-namespace

there 2 ways use clr-namespace:

  • if namespace same assembly xaml defined in:

    xmlns:local="clr-namespace:mynamespace"

  • if namespace different assembly (you must reference other assembly in project xaml being used):

    xmlns:local="clr-namespace:mynamespace;assembly=myassemblyname"

in case, looks both types in same assembly, you'd use first method of doing so. if you're getting error type can not found, because needs public type, , yours internal (the default protection level types in c#).

just emphasis, i'll quote msdn article:

clr-namespace: clr namespace declared within assembly contains public types expose elements.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -