【问题标题】:ScrollView into ContentPage pushed Async doesnt work滚动视图进入内容页面推送异步不起作用
【发布时间】:2017-07-29 05:30:30
【问题描述】:

我需要你的帮助。我实际上是从 Xamarin.forms 开始的。 我有一个 HomePage:TabbedPage,它有 3 个 ContentPage。

这 3 个页面中的一个是 ListView,在点击项目时,它会调用另一个带有 ScrollView 的内容页面。

ListView.ItemTapped += async (o, e) =>
{
    var myList = (ListView)o;
    var newPage = (myList.SelectedItem as Object);
    await Navigation.PushAsync(new Page(Object));
    myList.SelectedItem = null; // de-select the row
};

我在这个新页面上有一个 ScrollView,但它不起作用。

我复制了页面并在没有 Navigation.PushAsync 的情况下调用它,并且滚动工作。

我实际上只使用 iOS 模拟器。

你知道原因吗?

我正在使用 xaml。

非常感谢。如果您需要更多信息,请告诉我..!

还有我的 App.xaml.cs

public App()
    {
        InitializeComponent();

        MainPage = new HomePage();
    } 

这是我的主页:

public class HomePage : TabbedPage
{
    public HomePage()
    {
        var profilPage = new NavigationPage(new UserPage());
        profilPage.Title = "Profil";
        profilPage.Icon = "user.png";
        var gameListPage = new NavigationPage(new GameListPage());
        gameListPage.Title = "Jeux";
        gameListPage.Icon = "gamepad.png";
        Children.Add(profilPage);
        Children.Add(gameListPage);
     }
 }

我的主页调用 GameListPage :

<ContentPage.Content>
    <ListView x:Name="GameListView">
        <ListView.ItemTemplate>
          <DataTemplate> 
            <TextCell Text="{Binding name}" />

          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
</ContentPage.Content>

有事件:

GameListView.ItemTapped += async (o, e) =>
        {
            var myList = (ListView)o;
            var game = (myList.SelectedItem as Game);
            await Navigation.PushAsync(new GamePage(game));
            //await DisplayAlert("Tapped", game.name, "OK");
            myList.SelectedItem = null; // de-select the row
        };

GamePage 在这里:

gamePage.xaml

    <ContentPage.Content>
    <ScrollView>
        <RelativeLayout x:Name="RelativeLayout"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Grid RelativeLayout.WidthConstraint =
          "{ConstraintExpression Type=RelativeToParent,
                                 Property=Width,
                                 Factor=1,
                                 Constant=0}"
        RelativeLayout.HeightConstraint =
          "{ConstraintExpression Type=RelativeToParent,
                                 Property=Height,
                                 Factor=1,
                                 Constant=0}">
              <Grid.RowDefinitions>
                    <RowDefinition Height="500">
                    </RowDefinition>
                    <RowDefinition Height="500">
                    </RowDefinition>
                    <RowDefinition Height="550">
                    </RowDefinition>
                    <RowDefinition Height="20">
                    </RowDefinition>
                    <RowDefinition Height="300">
                    </RowDefinition>
                    <RowDefinition Height="500">
                    </RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*">
                    </ColumnDefinition>
                    <ColumnDefinition Width="*">
                    </ColumnDefinition>
                    <ColumnDefinition Width="*">
                    </ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.Children>

                    <StackLayout Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
                        <BoxView BackgroundColor="Black" HeightRequest="500"></BoxView>
                    </StackLayout>
                    <StackLayout Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">
                        <BoxView BackgroundColor="Black" HeightRequest="500"></BoxView>
                    </StackLayout>
                    <StackLayout Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
                        <BoxView BackgroundColor="Black" HeightRequest="500"></BoxView>
                    </StackLayout>
                </Grid.Children>
         </Grid>
          </RelativeLayout>
    </ScrollView>
</ContentPage.Content>

我把这个内容实际上是为了测试滚动。而且,在另一个 contentPage 中,具有相同的内容,我可以滚动。它就在这个页面上,通过 async Navigation.PushAsync 调用...

已编辑:解决了 StackLayout 与 HeightRequest 到 1500。我的滚动工作现在..临时解决所以..它不正确

【问题讨论】:

  • 你应该发布一些代码,或者更好的是,在 github 上的一个小仓库......你能发布“页面”代码或 xaml 吗?
  • 是的,我会在一段时间内完成。我将设置我的 ListView 代码和我的页面的 XAML。谢谢
  • 我编辑了它!没关系

标签: ios asynchronous xamarin.forms scrollview


【解决方案1】:

我建议您将BackgroundColor 设置为您的视图,例如:

ScrollView mainContainer = new ScrollView{ BackgroundColor = Color.Red };

因此,您可以仔细检查您的视图实际上是否正在渲染到 ContentPage 本身。

另外一个问题可能是 ScrollView 上没有足够的元素(Label, Image, Entry, Button...随便:https://developer.xamarin.com/guides/xamarin-forms/user-interface/controls/views/)来实际滚动。

为您的视图设置测试背景颜色,并让我们知道实际问题所在。

还请记住,ScrollView 有一个Contentproperty,因此您可以向它添加布局。示例:

var stack = new StackLayout();

for (int i = 0; i < 100; i++)
{
    stack.Children.Add(new Button { Text = "Button " + i });
}

MainPage = new ContentPage
{
    Content = new ScrollView { Content = stack }
};

【讨论】:

  • 我实际上是在使用 Xaml 作为我的视图。我用我的文件内容更新了我的帖子。如果需要,请检查它.. ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 1970-01-01
相关资源
最近更新 更多