none
Dependency Property declaration

    Question

  • public Data BioData {

    get { return (Data)GetValue(BioDataProperty); }

    set

    { SetValue(BioDataProperty,

    value); } }

    public

    static

     

    readonly DependencyProperty BioDataProperty = DependencyProperty.Register("BioData", "Data", "Mine.Details", new PropertyMetadata(Data.Age));

    Data is my enum and it throws an exception could you please any one help me ?

    David C

    Friday, September 23, 2011 5:04 AM

Answers

  • Hi All!  The DP registration in the developer preview uses a string, string approach rather than a type, type approach that you are familiar with.  FYI this has been changed back to type,type for compatability.  In he preview you can try this:

    readonly DependencyProperty BioDataProperty = DependencyProperty.Register("BioData", "Object", tpeof(Mine.Details).FullName, new PropertyMetadata(Data.Age));
    

    Notice the use of "Object" for the first type parameter.  We have some issues where the actual type isn't working well and using the generic "Object" is a workaround that works for some.  The second parameter I'd recommend using typeof(YourType).FullName.  This puts the proper name there as well as when things change you just can remove the .FullName portion.


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    Friday, September 23, 2011 2:43 PM

All replies

  • Same here. I don't understand the new signature of the Register method at all: why move from typeof to string? But I also get an exception.

    I'll dive into this.


    Dennis Vroegop Destrato Microsoft MVP Surface Please mark an answer as "answered" if it does help you!
    Friday, September 23, 2011 7:12 AM
  • Hi Denis,

     

    Thanks, Denis to make plateform independent they offer String instead of typeof(). For the simple property declaration like int , string it works fine. But when we come across Custom type property declaration like my enum. It throws an exception. I am not sure what may be solution. I tried in many ways. I guess there is no support for Custom type property declaration in WinRT. Could you please help me ? if u know about this ?

     

    Regards,

    David C


    • Edited by David alice DD Friday, September 23, 2011 7:19 AM typo mistake
    Friday, September 23, 2011 7:18 AM
  • The same problem here. This issue is really cumbersome. How to develop XAML applications without DependencyProperties?


    The explanation with platform independence is comprehensible. I read that there is a general problem with DependencyProperties in the Developer Preview, and that it will be fixed until Beta. Nevertheless I hope there is another (quicker) solution, otherwise WinRT isn't that usable yet.

    Friday, September 23, 2011 1:56 PM
  • Nevertheless I hope there is another (quicker) solution, otherwise WinRT isn't that usable yet.

    Yep. I can't get it to work after spending quite some time trying to figure it out. DPs seem to work fine with value types and strings, but not with custom reference types. Which makes the whole platform pretty useless beyond building simple applications.

    Can someone at Microsoft confirm this bug or is it just me, being too tired to see an obvious solution? (very well possible :-) )


    Dennis Vroegop Destrato Microsoft MVP Surface Please mark an answer as "answered" if it does help you!
    Friday, September 23, 2011 2:13 PM
  • Hi All!  The DP registration in the developer preview uses a string, string approach rather than a type, type approach that you are familiar with.  FYI this has been changed back to type,type for compatability.  In he preview you can try this:

    readonly DependencyProperty BioDataProperty = DependencyProperty.Register("BioData", "Object", tpeof(Mine.Details).FullName, new PropertyMetadata(Data.Age));
    

    Notice the use of "Object" for the first type parameter.  We have some issues where the actual type isn't working well and using the generic "Object" is a workaround that works for some.  The second parameter I'd recommend using typeof(YourType).FullName.  This puts the proper name there as well as when things change you just can remove the .FullName portion.


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    Friday, September 23, 2011 2:43 PM
  • Hi Devs, Hi Tim,

     

    Thanks for your experimentations & clarification, I was like a fly behind the glass with this blocking issue. Tim, I’ve tried your workaround (Object) and still get an exception. Here’s a stripped down snippet:

     

     

        partial class MainPage

        {

            public MainPage()

            {

                InitializeComponent();

     

                var test = new Test();  // Crash !

                test.Val = 0;

            }

           

            public class Test : UserControl

            {

                readonly DependencyProperty ValProperty = DependencyProperty.Register("Val", "int", typeof(Test).FullName, new PropertyMetadata(null));

                public int Val

                {

                    get { return (int)GetValue(ValProperty); }

                    set { SetValue(ValProperty, value); }

                }

            }

     

    Thanks for your help & hint, Clement

    Friday, September 23, 2011 3:17 PM
  • @Clement - unfortunately you are now hitting one of the rough edges around our story in the preview.  You need to use the control from XAML in order for the right code generation to occur.  The exception you are getting is because the XamlMetaDataPovider is not getting/providing the information needed to instance the type in code. :-(
    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    Friday, September 23, 2011 3:40 PM
  • Hello Tim,

     

    Ouch ! Really bad news for me for now ! In my evaluations, I’m trying to create/port UserControls with dynamic styles on the fly (therefore code behind + dynamic binding). Can you suggest any even “tedious” workaround in code ?

    If not, do you have a rough idea when to expect an update to preview release (weeks, months) ?

     

    Thanks for trying, Clement

    Friday, September 23, 2011 4:03 PM
  • Weird. This works on my machine, so I can instantiate an UserControl from code. However, setting the property from code doesn't work. But that's ok. I have the DependencyProperty so I can set animations and stuff from XAML anyway.

     

     


    Dennis Vroegop Destrato Microsoft MVP Surface Please mark an answer as "answered" if it does help you!
    Friday, September 23, 2011 6:05 PM
  • Thanks. This works. It's a workaround but I can live with that. I can happily continue now :-)
    Dennis Vroegop Destrato Microsoft MVP Surface Please mark an answer as "answered" if it does help you!
    Friday, September 23, 2011 6:05 PM
  • @ClementP - Unfortunately we aren't announcing any further timelines of updates at this time
    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    Friday, September 23, 2011 6:49 PM
  • Hi Tim,

     

    Thanks , it works for me .. Now it does not throw any exception when i declare the enum Biodata. But i face another problem . I am actually a custom control developer in WinRT. I declare the enum Biodata in my custom control. I just try to refer that enum for my control through the Metro style applications where my control used. But at that time it throws an XamlParseException that property "Biodata" not find in "Details". Could you please confirm me whether am i right ?. As of u told i had put "Object" in the first parameter and in the second it was typeof(Mine.Details).FullName. Could you please help me  to find the better solution.?

     

    Regards,

    David C

     

    Monday, September 26, 2011 4:23 AM
  • How about IValueConverter? Will the targetType parameter be a Type as it is in WPF/SL/WP7? I'm guessing that the culture parameter will remain a string since there is no CultureInfo class in WinRT?
    Thursday, October 13, 2011 7:15 PM
  • Hi Andrew,

    Yes. There is no culture info class in WInRT. Moreover i am not able declare the instance of the Converter in the generic.xaml. Did u get this problem ?.

    Regards,

    David C

    Friday, October 14, 2011 4:51 AM
  • I tried to keep it simple so I was not declaring the binding in xaml.
    Friday, October 14, 2011 11:10 AM
  • OK.

     

    Cheers,

    David C

    Friday, October 14, 2011 12:50 PM
  • This does not work for me.  I have this:

             ScaleFactorProperty = DependencyProperty.Register(
                   "ScaleFactor",
                   "Object",//typeof(double).ToString(),
                   typeof(ImageBox).FullName,
                   new PropertyMetadata(1.0, new PropertyChangedCallback(OnScaleFactorChanged)));
             //new PropertyMetadata(1.0)); 

     

    I get type initialization exception at runtime.

    Any suggestions?

    Friday, October 21, 2011 9:15 PM
  • Hi Ike,

     

    Is ImageBox your Template class ?. If you give the simple project na it  will be helpful to resovle?

     

    Regards,

    David C

    Monday, October 24, 2011 12:20 PM
  • int  Replace Int32 OK
    Monday, December 26, 2011 10:54 AM
  • Tim, this has changed in the CP again, correct?

    now its back to typeof(class)

    But there is one difference to the Silverlight Style:

    if the type is an "enum" in Silverlight you could put a string parameter to the control:

    ie: ....Register("MyProperty",typeof(<AnEnumClass>),typeof(<controlName>)...

    you could write in XAML:

    <controlName MyProperty="<OneOfTheEnumsInTheEnumClassAsClearText>"/>

    This does not work in the CP...it throws an error (Failed to create an "unknown" from text "OneOfTheEnumsInTheEnumClassAsClearText")

    Is there a workaround for this (except removing the enums and converting all to integers)?


    Chris Muench, MVP

    Tuesday, March 13, 2012 2:52 AM
  • @Chris - correct, it is the Type now.  I think there is a known issue with Enums in the CP.  Can you use typeof(object) in your Register and see if that helps?

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Tuesday, March 13, 2012 2:59 AM
  • I do typeof() and it compiles fine...but at runtime I get the error posted above.

    Looks like the typeof(enum) does not resolve correctly at runtime to the string given in Xaml


    Chris Muench, MVP

    Tuesday, March 13, 2012 3:23 AM
  • @Chris can you post more of your code -- we did a simple repro here on beta and it works, so we want to make sure we aren't missing something in your implementation.  feel free to send repro to timheuer at Microsoft as well.

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Tuesday, March 13, 2012 3:54 AM