Resizing frame and controls according to device size .Suggestions?

后端 未结 1 1626
-上瘾入骨i
-上瘾入骨i 2021-01-27 12:26

I have a frame with 2 buttons and label. What is the best practice to make sure the frame and controls inside resize according to the screen size?

Whatever I have tried

相关标签:
1条回答
  • 2021-01-27 12:53

    You could change the width and height according to your frame via binding Value Converters.

    Binding Value Converters: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters

    Set the name to your Frame first.

        <Frame
     …………
    x:Name="frame"/>
    

    Create the MyConverter. MyConverter.cs

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (double)value / 3.0;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Set the StaticResource.

     <ContentPage.Resources>
        <ResourceDictionary>
            <local:MyConverter x:Key="MyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    

    Binding to your button.

      <Button Grid.Row="0" Grid.Column="0" Text="A" 
                            WidthRequest="{Binding Source={x:Reference frame},Path=Width,Converter={StaticResource MyConverter}}"
                            HeightRequest="{Binding Source={x:Reference frame},Path=Height,Converter={StaticResource MyConverter}}"></Button>
                    <Button Grid.Row="0" Grid.Column="1" Text="B"  
                            WidthRequest="{Binding Source={x:Reference frame},Path=Width,Converter={StaticResource MyConverter}}"
                            HeightRequest="{Binding Source={x:Reference frame},Path=Height,Converter={StaticResource MyConverter}}"/>
    
    0 讨论(0)
提交回复
热议问题