XAML Binding and Attached Properties (AbsoluteLayout in my case)

Beachside_JasonBeachside_Jason USMember ✭✭
edited August 2015 in Xamarin.Forms

In XAML only I can't get this binding to work (the layout bounds don't get updated when the source property changes):
<BoxView x:Name="leftCropBox" LayoutBounds="{Binding LeftCropBox, Mode=OneWay}" AbsoluteLayout.LayoutFlags="None"/>
If I manually do the binding in code by listening for changes on my pseudo-view model (CropViewModel) it does work:
AbsoluteLayout.SetLayoutBounds(leftCropBox, CropViewModel.LeftCropBox);
The binding context is properly set on the view. Is there anyway to get this to work in XAML-only? I did try all the binding modes and leaving that off entirely.

Best Answers

Answers

  • Beachside_JasonBeachside_Jason USMember ✭✭

    Thanks for responding! I dropped the ball though - I didn't copy & paste. Sorry! I did have the XAML right in xamarin studio, and what's weird is that this binding works for my image but not for the boxviews (here it is copied & pasted):
    <Image x:Name="photoToCropImage" AbsoluteLayout.LayoutBounds="{Binding ImageBox}" AbsoluteLayout.LayoutFlags="None" Source="{Binding ImageSource}" /> <BoxView x:Name="topCropBox" AbsoluteLayout.LayoutBounds="{Binding TopCropBox}" AbsoluteLayout.LayoutFlags="None" BackgroundColor="White"/> <BoxView x:Name="leftCropBox" AbsoluteLayout.LayoutBounds="{Binding LeftCropBox}" AbsoluteLayout.LayoutFlags="None" BackgroundColor="White"/> <BoxView x:Name="rightCropBox" AbsoluteLayout.LayoutBounds="{Binding RightCropBox}" AbsoluteLayout.LayoutFlags="None" BackgroundColor="White"/> <BoxView x:Name="bottomCropBox" AbsoluteLayout.LayoutBounds="{Binding BottomCropBox}" AbsoluteLayout.LayoutFlags="None" BackgroundColor="White"/>
    I don't know what's wrong. For now binding through code works, but that's pretty ugly.

  • Beachside_JasonBeachside_Jason USMember ✭✭
    edited August 2015

    Wow - nice code! I'm definitely going to start using that nuget package for property change support, it's so much nicer. Thank you Lori!

    It took me a while to isolate the change that caused the difference in behavior that we're seeing. I simplified it to just one white box view on a black background.

    Here is the result using Xamarin Forms 1.3.3.6323 (looks good to me, one white line/boxview)

    and here is the result after updating to Xamarin Forms 1.4.4.6392 - no other changes were made, it's a black screen, no white box view :(

    Here is my xamarin solution for 1.3.3.6323 and here is the same thing using xamarin forms 1.4.4.6392. Note, after updating to 1.4.4.6392 I did get this error, but closing and re-opening XS seems to have fixed it: Error: Error initializing task FixedCreateCSharp

    Lori, I wasn't able to open your solution directly (I ran into some weird package restore issues). Are you using Visual Studio? I might have to pony up the $$ for that at some point, but right now it's just me and a friend developing on the side a few hours a week. Anyway, if you update your Xamarin Forms package do you still get the expected results?

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    I will take a look at it tonight and let you know the results.

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    @Beachside_Jason, something is definitely wrong on XF 1.4.4.6392 but in the iOS project only and only with attempting to set the LayoutBounds on the BoxView through binding. I was able to set the LayoutBounds on a Label in the AbsoluteLayout the same way, and it worked without issue on iOS.

    I also added a WP8 project into your solution using XF1.4.4.6392 and running your code, and the white line shows up there without problems. I haven't tried on Android yet, but that's what I've been able to uncover so far.

  • Beachside_JasonBeachside_Jason USMember ✭✭

    Thank you Lori - I owe you one! :smile:
    That is really funky. I'll try to submit a bug against the latest XF but I'm about to be out on travel for a while.

  • Beachside_JasonBeachside_Jason USMember ✭✭

    I've submitted my first XF bug report: https://bugzilla.xamarin.com/show_bug.cgi?id=33113
    Thanks again Lori!

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    You're welcome @Beachside_Jason!

  • NeilAldredNeilAldred NZMember
    edited September 2015

    I'm having a related problem. I've got a small image that I need to move around within a large absolute layout, I was trying to bind the layoutbounds to properties in a controller object but can't it to work. What sort of data type is required ?? help.

    Using Jasons code as example I guess I need to know what type of data I need to be in the ImageBox property and how to define it......

    AbsoluteLayout.LayoutBounds="{Binding ImageBox}"

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭
    edited September 2015

    @NeilAldred, LayoutBounds expects a Rectangle structure. Xamarin's online documentation provides samples on how to set it in code and within the XAML markup: https://developer.xamarin.com/api/type/Xamarin.Forms.AbsoluteLayout/

  • Beachside_JasonBeachside_Jason USMember ✭✭

    For anyone else who comes along this thread the latest version of Xamarin Forms (2.0.1.6495) resolves the iOS data-binding bug.

Sign In or Register to comment.