0

正如我已经在 Stackoverflow 上的另一篇文章中描述的那样,我试图获得不同的布局(一个框架跨越多个列表视图项)。现在我决定尝试以下方法:我的 ViewModel 是一个列表列表(就像分组列表视图一样)。但是,我没有使用分组列表视图,而是有一个普通的 ListView,一旦 ParentViewCell 的 bindingContext 可用,就会在代码隐藏中创建子列表的单个项目:

private void CommentViewCell_BindingContextChanged(object sender, EventArgs e)
        {
            if (this.BindingContext == null) return;
            var model = this.BindingContext as CommentViewModel;


            DateCommentViewCell dateCell = new DateCommentViewCell
            {
                BindingContext = model
            };

            ParentCommentViewCell parentCell = new ParentCommentViewCell
            {
                BindingContext = model
            };

            ContentStackView.Children.Add(dateCell.View);
            ContentStackView.Children.Add(parentCell.View);

            foreach (CommentBaseViewModel cbvm in model)
            {
                if (cbvm is CommentViewModel)
                {
                    ChildCommentViewCell childCell = new ChildCommentViewCell
                    {
                        BindingContext = cbvm
                    };

                    ContentStackView.Children.Add(childCell.View);
                }
            }
        }

当我运行它时,视觉效果实际上还不错,看看我的意图。

但是 BindingContext 是错误的: ChildCommentViewCell BindingContext 没有引用子的 CommentViewModel ,而是显示时父的 CommentViewModel。我像这样检查了 ChildCommentViewCell 的 BindingContext

public ChildCommentViewCell ()
        {
            InitializeComponent ();
            BindingContextChanged += ChildCommentViewCell_BindingContextChanged;

        }

        private void ChildCommentViewCell_BindingContextChanged(object sender, EventArgs e)
        {
            Debug.WriteLine("### ChildCommentViewCell BindingContext Changed");
            test();            
        }

        public void test()
        {
            var context = this.BindingContext as CommentViewModel;
            Debug.WriteLine("### Instance: " + this.GetHashCode());
            Debug.WriteLine("### \tBinding Context: " + context.CommentModel.Text);
            Debug.WriteLine("### \tLabel: " + ChildCommentText.Text);
        }

控制台上的输出就好了。但是,在我的手机上运行时,实际内容是(如上所述)ParentCommentViewModel 的内容。有任何想法吗?

ChildCommentViewCell 元素的 XAML 代码如下:

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.View.ViewCell.ChildCommentViewCell">
    <StackLayout Padding="10,0" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
        <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                <StackLayout Grid.Column="0" VerticalOptions="FillAndExpand" Orientation="Vertical" Spacing="0">
                    <Label Text="{Binding CommentModel.AuthorName}" Style="{StaticResource CommentAuthor}"/>
                </StackLayout>
                <Frame IsClippedToBounds="True" HasShadow="False" Margin="5" Padding="3" BackgroundColor="LightGray" CornerRadius="3.0">
                    <StackLayout Grid.Column="1" VerticalOptions="FillAndExpand" Orientation="Vertical" Spacing="0">
                        <Label x:Name="ChildCommentText" Text="{Binding Path=CommentModel.Text, StringFormat=' {0}'}" Style="{StaticResource CommentContent}"/>
                        <Label Text="{Binding CommentTimeAgo}" Style="{StaticResource CommentTime}" HorizontalOptions="Start"/>
                    </StackLayout>
                </Frame>
            </StackLayout>
        </StackLayout>
    </StackLayout>
</ViewCell>

另外一件事:我试图调试“出现”事件,但是这甚至没有被调用一次......?!

非常感谢您!

4

1 回答 1

0

在 BindingContextChanged 方法中发现了我的问题:我必须将 BindingContext 显式绑定到视图,而不仅仅是 ViewCell:

foreach (CommentBaseViewModel cbvm in model)
            {
                if (cbvm is CommentViewModel)
                {
                    ChildCommentViewCell childCell = new ChildCommentViewCell
                    {
                        BindingContext = cbvm
                    };
                    childCell.View.BindingContext = cbvm;
                    ContentStackView.Children.Add(childCell.View);
                }
}
于 2018-10-03T18:11:11.580 回答