How to find subviews of ContentView in Renderer

MilanLakhaniMilanLakhani USMember ✭✭

Hello there,

I have created one contentView in Xamarin.Form and added Label and buttons. I have made its renderer in ios with uiview. Now i want to access all labels and buttons in renderer with iOS. But when i check subviews of that view its shows zero views. Is there any way to access the all view which are added in content view in xamarin.forms in Renderer.

Thanks

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    What are you trying to do with the custom renderer?

  • MilanLakhaniMilanLakhani USMember ✭✭

    I want to make custom slider with my custom methods, which looks same on both platforms. So, i have created one content view and added component like boxView for horizontal line, button as a thumb of slider.

    After that i am applying gesture on thumb via renderer on both platforms. So, my concern is to access both boxView and button which i have added to the content view in forms, via viewRenderer of that content view.

    So, that i can update the frame of view and thumb according to dragging of thumb via gesture.

    I have tried to change layout in Forms but i am getting some issues, and i can't find the exact solution for that. I have posted the
    question on forum, but no replies till now.

    Link of my another Que. : https://forums.xamarin.com/discussion/37513/problem-with-xamarin-forms-viewextensions-layoutto-method-returns-false

    Thanks,

  • adamkempadamkemp USInsider, Developer Group Leader mod

    My suggestion is to do as much as you can in the cross-platform code. In that case that might mean making a custom ContentView that wraps the slider and adds the decorations around it. This can probably be done in XAML without a custom renderer. For the slider gesture you can make a custom renderer for just the core slider part, and that part can be just a very simple extension of the existing slider renderer. That way you don't have to deal with issues like layout and accessing children in your custom renderer, and you share as much code as possible.

  • MilanLakhaniMilanLakhani USMember ✭✭

    @adamkemp
    Thanks for reply,

    I have started developement with the same approach as you describe above. But by doing that i have difficulties in changing position and layout of the content view with 'ViewExtentions.LayoutTo' method. and some time it gives weird behavior in lay-outing the view and buttons.

    One question is, Is 'ViewExtentions.LayoutTo' proper method to update layout(Frame) of view in xamarin.Forms, or to update the view's layout or frame, we need to make renderer and do the same.

    My Current architecture is something like this.

    • Created ContentView and added a box view for horizontal line
    • button for the thumb or we can say pointer of slider
    • Another boxview to show progress (over main the horizontal line, with different color to highlight current progress)
    • Also updating the title of button with current value.

    • For gesture, made custom renderer of button as thumb on both platforms

    • Applied pan gesture on that button
    • After getting offset of moving i am updating Layout with LayouTo method on forms to update position of thumbButton and width of boxView to show progress
    • But after LayoutTo method if i update the title of the button for current value of slider it gives wiered behavior and thumb moves to its original position. Means it invalidates its layout.

    • So, i am updating frame form Forms code and updating title of thumb with current value from Renderer.

    • Now this thing works fine in iOS, but in android, in some Android OS its works fine but in some OS version it's not working.

    So, final question is, whether the above approach is good, or is there anything better approach to achieve the same. The main problem is, there is no any specific description of methods in xamrinApiDoc so we have check with trial and error. This is bit frustrating with working with Forms. I am stuck on this about a week but not getting proper solution.

    Hope you will help me :smile:

    Thanks,

  • adamkempadamkemp USInsider, Developer Group Leader mod

    LayoutTo is an animation method. You shouldn't use it for normal layout. You should be using a layout container (maybe AbsoluteLayout), and then you can just update the attached properties that the layout container looks at.

  • MilanLakhaniMilanLakhani USMember ✭✭

    Didn't get ur point. Can you elaborate more?
    One que. I have used relative layout in this, so is there any way to change the constraints which i have given in xaml file.
    Like we does in iOS by making its layout and updating it.

    Once again thanks.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    It would help if you could attach a sample project showing me what you're trying now.

  • MilanLakhaniMilanLakhani USMember ✭✭

    Actually i am not on my desk . So i cant give you code right now.
    But one general question : how to change constraints which are given in xaml from cs file?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You can set constraints in code using the static method RelativeLayout.SetBoundsConstraint (or similar method for other constraints). You pass in the object that you are positioning and the new constraint. This requires giving the view a name using x:Name in the XAML.

  • MilanLakhaniMilanLakhani USMember ✭✭
    edited April 2015

    Hi, @adamkemp

    I have tried the same, but it doesn't update the constraints. Is there any specific reoutins shoud i have to follow, or is it known bug.

    Thanks

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Could you post a .zip of a solution showing what you're trying to do?

  • MilanLakhaniMilanLakhani USMember ✭✭

    Hi Adam,

    Thanks, for reply. due to some company policy i am not able to post the zip file.

    Thanks,

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I'm not asking for the source to your real application. Just make a new solution that shows just this behavior. If you can't do that then there's not much I can do to help you.

Sign In or Register to comment.