.net - Can Generics be used with Silverlight? -
i have user control displays objects of client-declared type specialized behavior. use generics. however, i'm not sure how declare in xaml:
<local:editablelistbox x:name="sectionlist" margin="56,8,15,0" fontsize="64" selectionchanged="sectionlist_selectionchanged" />
listbox
uses object
members, makes me think perhaps there's no way have type safety here. or there?
(i'm building windows phone 7 app, if makes difference.)
update: i'm totally fine not having generics in xaml, i'm still trying figure out how set in code-behind. parameterized everything, it's still complaining.
code behind:
public partial class editablelistbox<t> : usercontrol, inotifypropertychanged t : ieditablelistmember { public editablelistbox() { // error: name 'initializecomponent' not exist in current context initializecomponent(); loaded += new routedeventhandler(editablelistbox_loaded); } // ... public int selectedindex { { // error: name 'contentlistbox' not exist in current context return contentlistbox.selectedindex; } set { // error: name 'contentlistbox' not exist in current context contentlistbox.selectedindex = value; } }
the xaml:
<grid x:name="layoutroot"> <listbox x:name="contentlistbox"> <listbox.itemtemplate> <datatemplate> <grid manipulationcompleted="grid_manipulationcompleted"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="*" /> </grid.columndefinitions> <image source="{binding iconsource}" grid.column="0" width="96" height="96" verticalalignment="center" visibility="{binding editing, converter={staticresource visibilityconverter}}" /> <textblock text="{binding name}" grid.column="1" foreground="{binding enabled, converter={staticresource enabledconverter}}" /> </grid> </datatemplate> </listbox.itemtemplate> </listbox> </grid>
it gives 2 compiler errors: listbox
contentlistbox
, initializecomponent()
not defined. suspect problem has how class split 2 partial
definitions, , mine parameterized whereas generated code not. how can around this?
no can't use generic types in silverlight xaml directly.
but familiarize mvvm pattern. models , viewmodels can generic types, , can coding there. xaml views dumb, have no or few code behind, , bind view models.
Comments
Post a Comment