0

I have circular buttons and am trying to change the size of them depending on if they are on phone or tablet using AbsolutLayout.

Here is my xaml:

<Frame BackgroundColor="{StaticResource ThemeColor}" Padding="0" CornerRadius="40" AbsoluteLayout.LayoutBounds="{StaticResource HomeCircle}" AbsoluteLayout.LayoutFlags="PositionProportional"> 
    <Label Text="&#xf5a0;" HorizontalOptions="Center" VerticalOptions="Center" Style="{StaticResource Icon}" TextColor="{StaticResource LabelColor}"/> 
    <Frame.GestureRecognizers> 
        <TapGestureRecognizer Tapped="Directions"/> 
    </Frame.GestureRecognizers> 
</Frame> 

<AbsoluteLayout.Resources> 
    <ResourceDictionary> 
        <OnIdiom x:Key="HomeCircle" x:TypeArguments="Frame" Phone=".85,.375, 80, 80"
                                                            Tablet=".85,.375, 120, 120"/> 
    </ResourceDictionary> 
</AbsoluteLayout.Resources>

I looked at this resource and it did not work:

How to use attached properties such as Grid.Row, AbsoluteLayout.LayoutFlags to OnPlatform tag in Xamarin Forms

Edit: Have tried TypeArguments of Rectangle, AbsoluteLayout, and Frame and they all give this error:

Error XFC0009 No property, BindableProperty, or event found for "Phone", or mismatching type between value and property.

Edit: Have also looked at this thread and it provides the same error:

https://xamarin.github.io/bugzilla-archives/55/55183/bug.html

Cfun
  • 8,442
  • 4
  • 30
  • 62
DeZLearnsPCs
  • 81
  • 2
  • 9
  • why is your TypeArgument "Frame"? – Jason Jan 18 '21 at 21:31
  • @Jason Because the frames are the actual circles with the tap gesture recognizers that act as buttons. Should my TypeArgument be AbsoluteLayout instead? Because I tried that and it gives me the same error. – DeZLearnsPCs Jan 18 '21 at 21:34
  • you are applying this to the LayoutBounds property of the Frame, which is type Rectangle – Jason Jan 18 '21 at 21:43
  • @Jason Just tried to change it, giving me the same error: Error XFC0009 No property, BindableProperty, or event found for "Phone", or mismatching type between value and property. At this point have tried TypeArguments of Rectangle, Frame, and AbsoluteLayout and they all give this error. – DeZLearnsPCs Jan 18 '21 at 21:46
  • @Jason If you refer to the code, it is. – DeZLearnsPCs Jan 18 '21 at 22:03

1 Answers1

1

I faced the same problem in the past. My solution is to set the AbsoluteLayout bounds by Binding in place of StaticResource.

In the ViewModel:

public Xamarin.Forms.Rectangle HomeCircle => Xamarin.Forms.Device.Idiom == TargetIdiom.Phone ? new Xamarin.Forms.Rectangle(0.5f, 0.5f, 80, 80) : new Xamarin.Forms.Rectangle(0.5f, 0.5f, 120, 120);

In the yaml:

<AbsoluteLayout>
   <Frame BackgroundColor="Gray"
      Padding="0"
      CornerRadius="40"
      AbsoluteLayout.LayoutBounds="{Binding HomeCircle}"
      AbsoluteLayout.LayoutFlags="PositionProportional">
      <Label Text="TEST"
         HorizontalOptions="Center"
         VerticalOptions="Center"
         TextColor="Black"/>
         <Frame.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding DoSomething}"/>
         </Frame.GestureRecognizers>
      </Frame>
   </AbsoluteLayout>

Anyway AbsoluteLayout.LayoutBounds requires Rectangle, and markup doesn't always allow everything :)

AndreaGobs
  • 338
  • 2
  • 18
  • Thank you Andrea! When I try to implement this, the position of the frame doesn't match the rectangle I create in code-behind, and when anything is done it throws an error: StaticResource not found for key HomeCircle. Any idea why? – DeZLearnsPCs Jan 19 '21 at 15:36
  • Nevermind! Just had to put in my ViewModel instead of code-behind, thanks for the help! – DeZLearnsPCs Jan 19 '21 at 15:52
  • 1
    Yes, I also put it int the ViewModel. It's not clear in the answer, I fix it – AndreaGobs Jan 19 '21 at 16:06