Xamarin.Forms同一页导航

时间:2018-10-22 13:24:12

标签: c# xaml xamarin.forms

我正在寻找更改页面内容的最佳方法,而无需导航到新页面。 我尝试了两个堆栈布局,然后按一下按钮,就可以更改每个堆栈布局的IsVisible和IsEnabled属性。尽管此方法可行,但在每个布局的末尾都留有一个小的空白(我相信这是Xamarin.Forms的错误)。

完成此任务的最佳方法是什么? Xamarin.Forms中有什么功能可以实现我错过的功能吗?

这里有一个草图设计供您了解我的意思: content 1 content 2

在建议我使用选项卡之前,我将添加我在应用程序中已经具有选项卡的内容,但是该草图未显示该内容。我需要这种导航才能仅在一页上工作。

我以前使用的无效代码是: (在任何人提到糟糕的命名约定和内容不足之前,我必须将其全部删除,因为它是为雇主编写的代码。

C#:

private void Button1_Clicked(object sender, EventArgs e)
{
    Content2.IsVisible = false;
    Content2.IsEnabled = false;
    Content1.IsVisible = true;
    Content1.IsEnabled = true;
}

private void Button2_Clicked(object sender, EventArgs e)
{
    Content2.IsVisible = true;
    Content2.IsEnabled = true;
    Content1.IsEnabled = false;
    Content1.IsVisible = false;
}

XML:

<ScrollView x:Name="content1" VerticalOptions="FillAndExpand" BackgroundColor="#f2f2f2">
<StackLayout Spacing="0">
    <StackLayout Orientation="Horizontal" BackgroundColor="White" Padding="20,20,20,20" HorizontalOptions="FillAndExpand">
        <StackLayout>
            <Label Text="text:" FontFamily="{StaticResource BoldFont}"/>
            <StackLayout Orientation="Horizontal">
                <Image x:Name="content1image" HeightRequest="25" WidthRequest="25"/>
                <Label x:Name="content1label" FontFamily="{StaticResource Font}" FontSize="27" TextColor="#969696"/>
            </StackLayout>
        </StackLayout>

        <StackLayout HorizontalOptions="FillAndExpand">
            <Entry x:Name="content1Entry" Keyboard="Numeric" Margin="0,25,0,0" Placeholder="0.00000000" FontFamily="{StaticResource Font}" FontSize="27" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="End" TextColor="#969696"/>
            <Label x:Name="content1Label2" FontSize="14" FontFamily="{StaticResource Font}" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="End" TextColor="#969696"/>
        </StackLayout>
    </StackLayout>

    <StackLayout Padding="20,30,20,0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Label Text="content1Label3" FontFamily="{StaticResource Font}"/>
        <StackLayout Orientation="Horizontal">
            <Button x:Name="content1button" Image="image.png" BackgroundColor="Transparent" HorizontalOptions="Start" Margin="0" WidthRequest="25" HeightRequest="25"/>
            <Entry x:Name="content1Entry2" FontFamily="{StaticResource Font}" FontSize="12" HorizontalOptions="FillAndExpand" HorizontalTextAlignment="End"/>
        </StackLayout>
    </StackLayout>

    <StackLayout VerticalOptions="EndAndExpand" Padding="0,-1,0,0">
        <Label x:Name="content1Label4" FontSize="19" HorizontalOptions="CenterAndExpand" FontFamily="{StaticResource Font}"/>
        <Label x:Name="content1Label5" FontSize="12" TextColor="#b6b6b6" HorizontalOptions="CenterAndExpand" FontFamily="{StaticResource Font}"/>

        <Button x:Name="content1Button2" VerticalOptions="End" HorizontalOptions="FillAndExpand" BorderRadius="25" BackgroundColor="#2r432d" BorderColor="#2r432d" TextColor="White" FontFamily="{StaticResource Font}" FontSize="20" BorderWidth="3" Margin="10,10,10,10"/>
    </StackLayout>

</StackLayout>

                                                                       

1 个答案:

答案 0 :(得分:1)

我使用Stacklayouts并没有取得很大的成功。但是,网格具有很多可定制性,在我看来,它可以扩展为填充您希望的所有区域。

这就是我要做的。

Xaml

<Grid x:Name="Grid1" IsVisible="False" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <Label FontSize="Medium" Text="Grid1 Label"/>
    </Grid>
    <Grid Grid.Row="1" HeightRequest="100" WidthRequest="375" VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand">
        <Button x:Name="btnGrid1"/>
    </Grid>
</Grid>

<Grid x:Name="Grid2" IsVisible="False" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <Label FontSize="Medium" Text="Grid2 Label"/>
    </Grid>
    <Grid Grid.Row="1" HeightRequest="100" WidthRequest="375" VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand">
        <Button x:Name="btnGrid2"/>
    </Grid>
</Grid>

隐藏代码

private void Button1_Clicked(object sender, EventArgs e)
{
    Grid2.IsVisible = false;
    Grid1.IsVisible = true;
}

private void Button2_Clicked(object sender, EventArgs e)
{
    Grid2.IsVisible = true;
    Grid1.IsVisible = false;
}
相关问题