c# - WP8 - ValidatesOnExceptions -


i can't work using vs2012 windows phone. if create text box property validateonexceptions=true , throw exception in setter, exception not caught comes out in debug window:

a first chance exception of type 'system.argumentexception' occurred in phoneapp1.dll exception of type 'system.argumentexception' occurred in phoneapp1.dll not handled in user code system.windows.data error: cannot save value target source. bindingexpression: path='thingy' dataitem='phoneapp1.mycontext' (hashcode=38891250); target element 'system.windows.controls.textbox' (name=''); target property 'text' (type 'system.string').. system.reflection.targetinvocationexception: exception has been thrown target of invocation. ---> system.argumentexception: said don't type 'foo'!    @ phoneapp1.mycontext.set_thingy(string value)    --- end of inner exception stack trace ---    @ system.runtimemethodhandle.invokemethod(object target, object[] arguments, signature sig, boolean constructor)    @ system.reflection.runtimemethodinfo.unsafeinvokeinternal(object obj, object[] parameters, object[] arguments)    @ system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture)    @ system.reflection.runtimepropertyinfo.setvalue(object obj, object value, bindingflags invokeattr, binder binder, object[] index, cultureinfo culture)    @ system.reflection.runtimepropertyinfo.setvalue(object obj, object value, object[] index)    @ system.windows.clrpropertylistener.set_value(object value)    @ system.windows.propertyaccesspathstep.set_value(object value)    @ system.windows.data.bindingexpression.updatevalue(). 

i made toy example isolate issue - here's 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:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:my="clr-namespace:phoneapp1;assembly=phoneapp1"     mc:ignorable="d"     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-->         <stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28">          <textblock text="do not type 'foo' in box"/>       <textbox text="{binding thingy, mode=twoway,  validatesonexceptions=true}"/>         </stackpanel>          <!--contentpanel - place additional content here-->         <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0">          </grid>      </grid>  </phone:phoneapplicationpage> 

my main page:

/* imports snipped..*/  namespace phoneapp1 {    public class mycontext {     private string _thingy = string.empty;     public string thingy {       {         return _thingy;       }       set {         if (value == "foo") {           throw new argumentexception("i said don't type 'foo'!");         }         _thingy = value;       }     }     public mycontext() {     }   }   public partial class mainpage : phoneapplicationpage {       // constructor     public mainpage() {       initializecomponent();       this.datacontext = new mycontext();     }    } } 

i've scoured clues, seems pretty categorical should provide visual feedback when validateonexceptions=true, , supported wp8:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/cc278072(v=vs.105).aspx#bkmk_datavalidation

our program terminator validation of geo-coordinates using ierrorinfo , friends.

you'll find validation mechanism working, you'll need add correct styles visual state manager in control template. here's our validating textbox stye

  <style x:key="validatingtextbox" targettype="textbox">     <setter property="fontfamily" value="{staticresource phonefontfamilynormal}"/>     <setter property="fontsize" value="{staticresource phonefontsizemediumlarge}"/>     <setter property="background" value="{staticresource phonetextboxbrush}"/>     <setter property="foreground" value="{staticresource phonetextboxforegroundbrush}"/>     <setter property="borderbrush" value="{staticresource phonetextboxbrush}"/>     <setter property="selectionbackground" value="{staticresource phoneaccentbrush}"/>     <setter property="selectionforeground" value="{staticresource phonetextboxselectionforegroundbrush}"/>     <setter property="borderthickness" value="{staticresource phoneborderthickness}"/>     <setter property="padding" value="2"/>     <setter property="template">       <setter.value>         <controltemplate targettype="textbox">           <grid background="transparent">             <visualstatemanager.visualstategroups>               <visualstategroup x:name="validationstates">                 <visualstate x:name="valid" >                   <storyboard duration="00:00:01">                     <objectanimationusingkeyframes storyboard.targetproperty="visibility"                                        storyboard.targetname="warningellipse">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>collapsed</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility"                                        storyboard.targetname="warningtext">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>collapsed</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                   </storyboard>                 </visualstate>                 <visualstate x:name="invalidunfocused">                   <storyboard duration="00:00:01">                     <objectanimationusingkeyframes storyboard.targetproperty="visibility"                                        storyboard.targetname="warningellipse">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>visible</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                     <!-- note warning text not shown if unfocused -->                     <objectanimationusingkeyframes storyboard.targetproperty="visibility"                                        storyboard.targetname="warningtext">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>collapsed</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                   </storyboard>                 </visualstate>                 <visualstate x:name="invalidfocused">                   <storyboard>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility"                                        storyboard.targetname="warningtext">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>visible</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility"                                        storyboard.targetname="warningellipse">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>visible</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                   </storyboard>                 </visualstate>               </visualstategroup>               <visualstategroup x:name="commonstates">                 <visualstate x:name="normal"/>                 <visualstate x:name="mouseover"/>                 <visualstate x:name="disabled">                   <storyboard>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility" storyboard.targetname="enabledborder">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>collapsed</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility" storyboard.targetname="disabledorreadonlyborder">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>visible</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                   </storyboard>                 </visualstate>                 <visualstate x:name="readonly">                   <storyboard>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility" storyboard.targetname="enabledborder">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>collapsed</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="visibility" storyboard.targetname="disabledorreadonlyborder">                       <discreteobjectkeyframe keytime="0">                         <discreteobjectkeyframe.value>                           <visibility>visible</visibility>                         </discreteobjectkeyframe.value>                       </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="background" storyboard.targetname="disabledorreadonlyborder">                       <discreteobjectkeyframe keytime="0" value="{staticresource phonetextboxbrush}"/>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="borderbrush" storyboard.targetname="disabledorreadonlyborder">                       <discreteobjectkeyframe keytime="0" value="{staticresource phonetextboxbrush}"/>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="foreground" storyboard.targetname="disabledorreadonlycontent">                       <discreteobjectkeyframe keytime="0" value="{staticresource phonetextboxreadonlybrush}"/>                     </objectanimationusingkeyframes>                   </storyboard>                 </visualstate>               </visualstategroup>               <visualstategroup x:name="focusstates">                 <visualstate x:name="focused">                   <storyboard>                     <objectanimationusingkeyframes storyboard.targetproperty="background" storyboard.targetname="enabledborder">                       <discreteobjectkeyframe keytime="0" value="{staticresource phonetextboxeditbackgroundbrush}"/>                     </objectanimationusingkeyframes>                     <objectanimationusingkeyframes storyboard.targetproperty="borderbrush" storyboard.targetname="enabledborder">                       <discreteobjectkeyframe keytime="0" value="{staticresource phonetextboxeditborderbrush}"/>                     </objectanimationusingkeyframes>                   </storyboard>                 </visualstate>                 <visualstate x:name="unfocused"/>               </visualstategroup>             </visualstatemanager.visualstategroups>             <border x:name="enabledborder"                   borderbrush="{templatebinding borderbrush}"                   borderthickness="{templatebinding borderthickness}"                   background="{templatebinding background}" margin="{staticresource phonetouchtargetoverhang}">               <grid>                 <grid.rowdefinitions>                   <rowdefinition height="auto" />                   <rowdefinition height="auto"/>                 </grid.rowdefinitions>                 <contentcontrol grid.row="0" x:name="contentelement" borderthickness="0" horizontalcontentalignment="stretch"                         margin="{staticresource phonetextboxinnermargin}"                         padding="{templatebinding padding}"                         verticalcontentalignment="top"/>                 <ellipse grid.row="0"                     x:name="warningellipse"  visibility="collapsed"                     margin="12,12,12,0"                     verticalalignment="top"                     horizontalalignment="right"                     width="14" height="14"                     stroke="#40000000" strokethickness="2" fill="red">                 </ellipse>                 <textblock x:name="warningtext" grid.row="1" foreground="red"  visibility="collapsed" textwrapping="wrap"                         margin="3,-12,3,0"                          text="{binding relativesource={relativesource templatedparent},                                path=(validation.errors)[0].errorcontent, fallbackvalue=''}"></textblock>               </grid>             </border>             <border x:name="disabledorreadonlyborder" borderbrush="{staticresource phonedisabledbrush}"                   borderthickness="{templatebinding borderthickness}" background="transparent"                   margin="{staticresource phonetouchtargetoverhang}" visibility="collapsed">               <textbox x:name="disabledorreadonlycontent"                      background="transparent"                      foreground="{staticresource phonedisabledbrush}"                      fontweight="{templatebinding fontweight}" fontstyle="{templatebinding fontstyle}"                      fontsize="{templatebinding fontsize}" fontfamily="{templatebinding fontfamily}"                      isreadonly="true" selectionforeground="{templatebinding selectionforeground}"                      selectionbackground="{templatebinding selectionbackground}" textalignment="{templatebinding textalignment}"                      textwrapping="{templatebinding textwrapping}" text="{templatebinding text}"                      template="{staticresource phonedisabledtextboxtemplate}"/>             </border>           </grid>         </controltemplate>       </setter.value>     </setter>   </style> 

Comments