简体   繁体   中英

Xamarin.Forms Same Page Navigation

I'm looking for the best way to change the content of a page, without navigating to a new one. I tried having two stacklayouts, and on a button press I'd change the IsVisible and IsEnabled properties of each stack layout. Although this worked, I was left with a small white gap at the end of each layout (I believe this is a Xamarin.Forms bug).

What would be the best way to accomplish this task? Is there anything built into Xamarin.Forms that can do this that I have missed?

Here is a little sketch design for you to see what I mean: 内容一 内容二

Before suggesting I use tabs, I'll add that I already have tabs in the application, the sketch doesn't show that though. I need this navigation to work on only ONE page.

The code I used before, that didn't work is: (Before anybody mentions the poor naming conventions and lack of content, I had to strip it all out as it's code written for an employer.

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>

I have not had very much success using Stacklayouts. Grids however has a lot of customizability and in my case it expands to fill all the area that you wish for.

This is how I would do it.

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>

Code-behind

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM