【问题标题】:Xamarin.Forms Same Page NavigationXamarin.Forms 相同页面导航
【发布时间】:2018-10-22 13:24:12
【问题描述】:

我正在寻找无需导航到新页面即可更改页面内容的最佳方式。 我尝试了两个堆栈布局,按下按钮我会更改每个堆栈布局的 IsVisible 和 IsEnabled 属性。虽然这可行,但我在每个布局的末尾留下了一个小的白色间隙(我相信这是一个 Xamarin.Forms 错误)。

完成这项任务的最佳方法是什么? Xamarin.Forms 中是否有任何我错过的可以做到这一点的东西?

这里有一个小草图让你明白我的意思:

在建议我使用标签之前,我要补充一点,我在应用程序中已经有了标签,但草图没有显示。我需要这个导航只在一个页面上工作。

我之前使用的代码不起作用是: (在有人提到糟糕的命名约定和缺乏内容之前,我不得不把它全部去掉,因为它是为雇主编写的代码。

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>

【问题讨论】:

  • 您的解决方案是最好的。您所指的错误,它可能不是 xamarin 形式。您不太可能为您的视图提供正确的空间,这就是它们没有占据整页的原因。没有任何代码,很难提供答案。
  • 感谢您的反馈!我已经添加了之前的代码。

标签: c# xaml xamarin.forms


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多