How to bind class to xaml element and dynamically render childrens.

TrixoTrixo Member ✭✭
edited June 24 in Xamarin.Forms

Hi, I would like to make countdown which you initiate in following way.

  1. Countdown MySpecialCountdown= new Countdown(30) // 30 for seconds.
    Then I add it to the xaml as follows.
    MyXamlXname.Children.Add(MySpecialCountdown)

  2. Countdown MySpecialCountdown= new Countdown(30,MyXamlXName) // 30 for seconds and MyXamlXName as element is xaml where to bind this countdown.

Countdown should update everysecond without additional code out of Countdown.cs

Any idea how to achieve this?

Trixo

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I think you need to define some things better.

    1. When do you want the countdown to start? When the view is created, when it is actually displayed?
    2. "element is xaml where to bind this countdown" What does this mean? Just the layout you want to add this view to? Why does that need to be a constructor argument?
    3. What happens when the countdown finishes?
    4. What if you want to pause, cancel, or restart the countdown?
  • TrixoTrixo Member ✭✭

    Yes, Its little bit harder to explain. Sorry @JoeManke .
    Well, My countdown should take DateTime of future time. Lets say Its 14:00 now. I have datetime with value of 16:00 of same day.
    Which means 2 hours.

    The thing is countdown starts 20 minutes before the actuall value. In example case countdown should start 15:40, but the instance of countdown has to be created 8 hours before the end of countdown.

    What I can do is create Xaml document where I define all stuff I need and just hide the countdown until 20 minutes before time.

    But I dont want to do that. All I want is to bind to a xaml element which would act as a parent/container for all the items my countdown class generates, Items like Countdown progress bar and nummberal countdown.

    Only thing I dont know is how to bind parent element and insert stuff into it afterwards.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    So you want a class that just creates views and stuffs them into an arbitrary Layout, instead of arranging them into something presentable yourself?

  • TrixoTrixo Member ✭✭

    @JoeManke no, I just want a class that can insert(and be up to date with class properites) stacklayout full of items to parent element which is defined in xaml.

    So:
    1. I create new instance of the class
    2. Class binds itself to the stack layout(Defined in xaml file) which will be used as a container for other items.
    3. Class inserts another stack layout(Generated by class) into the parent stacklayout(from point 2.) and keeps everything up to date with class properties.

    Its like normal databinding bit instead of binding in to already created xaml elements I need to create them in class and insert them into xaml.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Do you want to add the control to the layout at a time interval?

  • TrixoTrixo Member ✭✭
    edited June 25

    @LeonLu I need to create a class that generates different kinds of xaml elements depending of what type of params i give, and insert those xaml elements inside a defined place (ex: inside a StackLayout) in Xamarin?

Sign In or Register to comment.