动态更改Source后,Xamarin Forms ImageButton会变小

时间:2019-02-16 21:07:13

标签: android xamarin.forms imagebutton

我在“语言页面”上有3个ImageButton,分别代表应用程序(Android)的3种不同语言。

<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="flagsImageButtonStyles" TargetType="ImageButton">
            <Setter Property="HeightRequest"
                    Value="130" />
            <Setter Property="WidthRequest"
                    Value="130" />
            <Setter Property="Aspect" Value="AspectFill" />
            <Setter Property="BackgroundColor" Value="Transparent" />
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1.5*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    ...

    <ImageButton Grid.Row="1" StyleId="fr" Grid.Column="1" x:Name="btnFrench" Clicked="LanguageButton_Clicked" Style="{StaticResource flagsImageButtonStyles}"/>
    <ImageButton Grid.Row="2" StyleId="nl" Grid.Column="1" x:Name="btnDutch" Clicked="LanguageButton_Clicked" Style="{StaticResource flagsImageButtonStyles}"/>
    <ImageButton Grid.Row="3" StyleId="en" Grid.Column="1" x:Name="btnEnglish" Clicked="LanguageButton_Clicked" Style="{StaticResource flagsImageButtonStyles}"/>

</Grid>

我这样设置ImageButtons的来源:

protected override void OnAppearing()
    {
        switch (CrossMultilingual.Current.CurrentCultureInfo.TwoLetterISOLanguageName) // "fr", "nl", "en"
        {
            case "fr":
                btnFrench.Source = "fr_flag_selected.png";
                btnDutch.Source = "nl_flag.png";
                btnEnglish.Source = "en_flag.png";
                break;

            case "nl":
                btnFrench.Source = "fr_flag.png";
                btnDutch.Source = "nl_flag_selected.png";
                btnEnglish.Source = "en_flag.png";
                break;

            case "en":
                btnFrench.Source = "fr_flag.png";
                btnDutch.Source = "nl_flag.png";
                btnEnglish.Source = "en_flag_selected.png";
                break;
        }
        base.OnAppearing();
    }

每当我单击其中之一时,我都会设置语言,然后导航到另一页。

因此,下次加载“语言页面”时,它将检查当前语言并改编ImageBUtton的来源。

问题在于,每当我单击ImageButton之一或应用程序进入睡眠状态时,ImageButton就会变得非常小。

任何想法,可能是什么问题?

1 个答案:

答案 0 :(得分:0)

是固定的。

我必须为图像按钮,行和列定义指定相同的Height / Width请求:

<Grid.RowDefinitions>
        <RowDefinition Height="1.5*"></RowDefinition>
        <RowDefinition Height="130"></RowDefinition>
        <RowDefinition Height="130"></RowDefinition>
        <RowDefinition Height="130"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="130"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>