Xamarin Forms - 识别位置

Xamarin Forms - Recognize Postition

我想创建一个只有在用户位于文章底部时才可见的评论条目。 因此应用程序必须识别用户何时滚动到足够多,然后一个方法应该使输入字段可见。

我在网上找不到这样的东西,所以也许你们可以帮助我。

This one is without the entryfield and when the user scrolls down ...

... the entryfield becomes visible

如果您使用的是 ScollView,则会在滚动视图时触发一个 Scrolled 事件,并且 ScrolledEventArgs 包含 ScrollXScrollY 属性,让您知道 ScrollView 当前的位置。如果将 ScrollYScrollViewContentSize 属性 的高度进行比较,例如:

XAML:

<StackLayout>
    <ScrollView x:Name="scrollView" Scrolled="Handle_Scrolled">
        <StackLayout>
            <Label Text="{Binding Article}" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand" />
        </StackLayout>
    </ScrollView>
    <Entry IsVisible="{Binding AtEnd}" Placeholder="End reached!" />
</StackLayout>

后面的代码(MainPage 是一个 ContentPage 子类):

string _article;
public string Article
{
    get
    {
        return _article;
    }
    set
    {
        if (_article != value)
        {
            _article = value;
            OnPropertyChanged("Article");
        }
    }
}

bool atEnd;
public bool AtEnd
{
    get
    {
        return atEnd;
    }
    set
    {
        if (atEnd != value)
        {
            atEnd = value;
            OnPropertyChanged("AtEnd");
        }
    }
}

public MainPage()
{
    Article = "<put in enough text here to force scrolling>";
    AtEnd = false;
    InitializeComponent();
    BindingContext = this;
}

void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e)
{
    if (e.ScrollY + scrollView.Height >= scrollView.ContentSize.Height)
        AtEnd = true;
    else
        AtEnd = false;
}

也就是说,为什么不使用相同的滚动视图将条目放在文章下方? IOW 只是将 Entry 元素放在上面 Label 之后的相同 StackLayout 中,条目将始终出现在末尾,但用户在向下滚动之前不会看到它.似乎那将是一个更简单的解决方案。当然,您可能没有使用 Label,但同样适用,只需将 Entry 放在 ScrollView 滚动的布局底部即可。