繁体   English   中英

Xamarin.Forms从数据模板中查找容器listview

[英]Xamarin.Forms find container listview from within a data template

我在一个视图中有一个ListView,其中包含从DataTemplateSelector上的自定义实现构建的数据模板的集合。

实现非常简单,JSON字符串表示我需要呈现的控件的类型,然后将对象转换为必要的类型(为清楚起见将其声明)。

我的问题(最近一整天一直在为此而苦苦挣扎)是从这些数据模板之一中获取ListView的句柄。 我需要这样做,以迭代每个数据模板,以查找与每个“实体”中包含的某些数据关联的匹配“关键字”。 用例的细节无关紧要,不幸的是,我似乎无法从ActionA或ActionZ中获取ListView的实例。

有没有一种方法可以通过后面的代码来获取数据模板容器的实例?

考虑到以下代码是使用Prism MVVM的应用程序的摘录,所有代码逻辑都是在ViewModel中开发的。

ListView看起来像这样(为简洁起见,省略了代码):

<ListView VerticalOptions="FillAndExpand" 
            BackgroundColor="{StaticResource ColorGreyDark}"
            x:Name="TemplSelector"
            AutomationId="TemplSelector"
            SeparatorVisibility="None" 
            ItemTemplate="{StaticResource QuestionnaireDataTemplateSelector}" 
            ItemsSource="{Binding QuestionnaireList}" 
            CachingStrategy="RecycleElement" HasUnevenRows="true">
    <ListView.Footer>
        <!-- Hosts an indicator and a button -->
    </ListView.Footer>
</ListView>

ItemTemplate是一个静态资源集合,然后将其分配给自定义数据模板选择器上的关联属性:

<ContentPage.Resources>
    <ResourceDictionary>
        <converters:InverseBooleanConverter x:Key="inverseBooleanConverter" />
        <DataTemplate x:Key="checkbox">
            <ViewCell>
                <templates:CheckBoxActionView />
            </ViewCell>
        </DataTemplate>
        <DataTemplate x:Key="dropDown">
            <ViewCell>
                <templates:DropDownActionView />
            </ViewCell>
        </DataTemplate>
        <DataTemplate x:Key="freeText">
            <ViewCell>
                <templates:FreeTextActionView />
            </ViewCell>
        </DataTemplate>

        <local:QuestionnaireDataTemplateSelector x:Key="QuestionnaireDataTemplateSelector" 
                                                 Checkbox="{StaticResource checkbox}" 
                                                 DropDown="{StaticResource dropDown}" 
                                                 FreeText="{StaticResource freeText}" />
    </ResourceDictionary>
</ContentPage.Resources>

后面的数据模板选择器代码如下所示:

public class QuestionnaireDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate Checkbox { get; set; }

    public DataTemplate DropDown { get; set; }

    public DataTemplate FreeText { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if (typeof(CheckBoxAction) == item.GetType())
        {
            return Checkbox;
        }
        else if (typeof(DropDownAction) == item.GetType())
        {
            return DropDown;
        }
        else if (typeof(FreeTextAction) == item.GetType())
        {
            return FreeText;
        }

        return null;
    }
}

上面的代码中的每个{Name}Action类都是具有关联的ViewModel的StackLayout XAML。

我的第一个价格是尝试从ListView控件中获取ItemsSource,比如说在“复选框”操作的ViewModel中,但是似乎没有办法在CheckboxAction视图模型中获得对ListView的引用,因此可以获取ItemsSource。

如果可以获取ListView实例,则可以执行所需的操作,但是在网络上找不到任何有关此的信息。

是否可能,如果可以,我将如何处理?

我在Prism 6+中走了EventAggregator路线。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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