Thursday, 15 August 2013

c# - Still can't use attached properties in silverlight for windows phone -



c# - Still can't use attached properties in silverlight for windows phone -

i'm total noob basic experience in javascript larn c#net programming create phone apps. app i'm trying needs me able set custom property on xaml elements.

i found seems simple exemple of on stackoverflow (adding custom attributes element in xaml?) , didn't work. read lot of documentation everywhere , sense more confused ever...i think got concept right, implementation not. exemple, if re-create thecode page mentionned, this:

mainpage.xaml

<phone:phoneapplicationpage x:class="phoneapp1.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone" xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone" xmlns:local="clr-namespace:mynamespace" //<--added me xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" d:designwidth="480" d:designheight="768" fontfamily="{staticresource phonefontfamilynormal}" fontsize="{staticresource phonefontsizenormal}" foreground="{staticresource phoneforegroundbrush}" supportedorientations="portrait" orientation="portrait" shell:systemtray.isvisible="true"> <!--layoutroot root grid page content placed--> <grid x:name="layoutroot" background="transparent"> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> </grid.rowdefinitions> <!--titlepanel contains name of application , page title--> <!--contentpanel - place additional content here--> <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0"></grid> </grid> </phone:phoneapplicationpage>

mainpage.xaml.cs

using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using microsoft.phone.controls; namespace phoneapp1 { public partial class mainpage : phoneapplicationpage { // constructor public mainpage() { initializecomponent(); } } namespace mynamespace { public static class myclass { public static readonly dependencyproperty mypropertyproperty = dependencyproperty.registerattached("myproperty", typeof(string), typeof(myclass), new frameworkpropertymetadata(null)); public static string getmyproperty(uielement element) { if (element == null) throw new argumentnullexception("element"); homecoming (string)element.getvalue(mypropertyproperty); } public static void setmyproperty(uielement element, string value) { if (element == null) throw new argumentnullexception("element"); element.setvalue(mypropertyproperty, value); } } } }

from there, can't add together visual element because have error:

error 1 type or namespace name 'frameworkpropertymetadata' not found (are missing using directive or assembly reference?)

now, if code worked, understand, attached property avalaible objetc of class... how bind xaml element , c#class?

if see i'd go, informatio have appreciated. spent hours on little detail...

many in advance!!!!!!!!!!!!! (yes, i'm noob...i have c#books ordered coming, checked many sites still nedd guidance..thanks again)

edit: follow up: suggestion given me worked , allow me compile but:

the xmlns:local="clr-namespace:mynamespace" gives me error:

undefined clr namespace. 'clr-namespace' uri refers namespace 'mynamespace' not included in assembly. need line?

from there can't add together other element. if remove line, , add together ellipse, exemple, can't give myproperty, vs tells me :

the property'myproperty' not found in type ellipse.

i understand registered 'myproperty' myclass. well, how give ellipse class of 'myclass'? should take approach? can utilize 'myproperty' on want? tips highly appreciated, thanks!

everything looks fine part. need alter frameworkpropertymetadata propertymetadata, , code should compile fine.

update: xaml namespace wrong. have defined mynamespace within phoneapp1 namespace. hence finish namespace phoneapp1.mynamespace

xmlns:local="clr-namespace:phoneapp1.mynamespace"

this article has more on xaml namespaces in silverlight

applying attached property simple plenty 1 time have namespace sorted. in fact, have used attached property

shell:systemtray.isvisible="true"

your attached property applied so:

<grid local:myclass.myproperty="value" />

this article attached properties should help

c# silverlight properties

No comments:

Post a Comment