微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 ContentView子视图获取一个属性值到 ContentPage父视图

如何解决从 ContentView子视图获取一个属性值到 ContentPage父视图

作为一名经验不足的 Xamarin 开发人员,我试图从父页面 (ContentView) 的子页面 (ContentPage) 中检索属性值。 我可以找到很多示例如何获取/设置从父页面到子页面的值,但不能反过来。

更多细节:

在我的 ContentPage 中,我有一个 CarouselView,这个 CarouselView一个 DataTemplate,其中包含一个 ContentView,这个 ContentView 里面也有一个 CarouselView 2 层/2 个垂直轮播项目。

当CarouselView在ContentView(子页面)中的位置改变到第二项时,父页面中的IndicatorView应该设置为invisible

我在使用 BindableProperty 方面经验不足,但我认为这是可行的方法。我现在设置如下:

页面/ContentPage:

<local:ExtendedCarouselView
    x:Name="carousel"
    HorizontalScrollBarVisibility="Never"
    IndicatorView="activityIndicatorView"
    IsScrollAnimated="False"
    ItemsSource="{Binding ActivityData}"
    Position="{Binding Position,Mode=TwoWay}"
    VerticalOptions="FillAndExpand">
    <local:ExtendedCarouselView.ItemTemplate>
        <DataTemplate>
            <Frame Style="{StaticResource CarouselWorkaround}">
                <local:PCSActivityOverviewTemplate x:Name="testy" />
            </Frame>
        </DataTemplate>
    </local:ExtendedCarouselView.ItemTemplate>
</local:ExtendedCarouselView>

<IndicatorView
    x:Name="activityIndicatorView"
    Padding="0,30"
    IndicatorColor="{DynamicResource TranslucidBlack}"
    IsVisible="{Binding InnerCarouselViewPosition,Converter={StaticResource IndicatorVisibilityConverter},Mode=TwoWay}"
    SelectedindicatorColor="{DynamicResource BaseTextColor}"
    VerticalOptions="Start" />

页面/ContenView (XAML):

<ContentView.Content>

    <CarouselView
        x:Name="carousel"

        ItemsSource="{Binding .,Converter={StaticResource OnetoManyConverter},ConverterParameter=2}"
        VerticalOptions="FillAndExpand"
        VerticalScrollBarVisibility="Never"
        PositionChanged="carousel_PositionChanged"> <!-- The event which should change the property 'InnerCarouselViewPosition' -->

        <CarouselView.ItemTemplate>
            <grial:IntMemberTemplateSelector MemberName="Position">
                <grial:IntMemberTemplateSelector.Items>

                    <!--  CAROUSEL'S PAGE 0  -->
                    <grial:IntMemberTemplateSelectorItem Value="0">
                        <DataTemplate>

                            <!-- Other elements... -->

                        </DataTemplate>
                    </grial:IntMemberTemplateSelectorItem>


                    <!--  CAROUSEL'S PAGE 1  -->
                    <grial:IntMemberTemplateSelectorItem Value="1">
                        <DataTemplate>

                            <!-- Other elements... -->

                        </DataTemplate>
                    </grial:IntMemberTemplateSelectorItem>

                </grial:IntMemberTemplateSelector.Items>
            </grial:IntMemberTemplateSelector>
        </CarouselView.ItemTemplate>

    </CarouselView>

</ContentView.Content>

ContenView (C#/.cs):

public partial class PCSActivityOverviewTemplate : ContentView
{
    public static BindableProperty CurrentChildCarouselViewLocationProperty =
       BindableProperty.Create(
           nameof(CurrentChildCarouselViewLocationProperty),typeof(int),typeof(CarouselView),defaultValue: 1);

    public int CurrentChildCarouselViewLocation
    {
        get { return (int)GetValue(CurrentChildCarouselViewLocationProperty); }
        set { SetValue(CurrentChildCarouselViewLocationProperty,value); }
    }

    private void carousel_PositionChanged(object sender,PositionChangedEventArgs e)
    {
        CarouselView _carouselView = (CarouselView)sender;
        CurrentChildCarouselViewLocationProperty = _carouselView.Position;  
    }

    ... code omitted

}

当内部轮播视图位置发生变化时,应设置 bindable property,该属性应在父页面中使用转换器设置指示器视图可见/不可见(位置 0 = 可见,位置 1 =无形的)。 对于某些人来说,可能是一个很明显的原因,上面的方法不起作用。

可见性转换器:

public class CarouselIndicatorVisibilityConverter : IValueConverter
{
    public object Convert(object value,Type targettype,object parameter,CultureInfo culture)
    {
        return (int)value != 1; 
    }

    public object ConvertBack(object value,CultureInfo culture)
    {
        return (bool)value ? 0 : 1;
    }
}

更新*

我也尝试在我的 viewmodel 中使用绑定作为 Position 属性,而绑定值发生变化时,我无法在父页面中访问它,没有任何反应,没有触发转换器),我删除了可绑定属性。>

新的内容页面 XAML(父轮播):

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    ...
    >
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:CarouselIndicatorVisibilityConverter x:Key="IndicatorVisibilityConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>

        <Grid>
            
            <Grid>
               
                <local:ExtendedCarouselView
                    x:Name="carousel"
                    HorizontalScrollBarVisibility="Never"
                    IndicatorView="activityIndicatorView"
                    IsScrollAnimated="False"
                    ItemsSource="{Binding ActivityData}"
                    Position="{Binding Position,Mode=TwoWay}"
                    VerticalOptions="FillAndExpand">
                    <local:ExtendedCarouselView.ItemTemplate>
                        <DataTemplate>
                            <Frame Style="{StaticResource CarouselWorkaround}">
                                <local:PCSActivityOverviewTemplate x:Name="testy" />
                            </Frame>
                        </DataTemplate>
                    </local:ExtendedCarouselView.ItemTemplate>
                </local:ExtendedCarouselView>

                <IndicatorView
                    x:Name="activityIndicatorView"
                    Padding="0,30"
                    IndicatorColor="{DynamicResource TranslucidBlack}"
                    IsVisible="{Binding BindingContext.CurrentChildCarouselViewLocation,Source={x:Reference carousel},Mode=TwoWay}"
                    SelectedindicatorColor="{DynamicResource BaseTextColor}"
                    VerticalOptions="Start" />


            </Grid>
        </Grid>

    </ContentPage.Content>
</ContentPage>

页面的 XAML:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    ...
    >
    
    <ContentView.Content>

        <CarouselView
            x:Name="carousel"
            IsBounceEnabled="False"
            ItemsSource="{Binding .,ConverterParameter=2}"
            Position="{Binding CurrentCarouselViewLocation}"
            PositionChanged="carousel_PositionChanged"
            VerticalOptions="FillAndExpand"
            VerticalScrollBarVisibility="Never">
            <CarouselView.ItemsLayout>
                <LinearItemsLayout
                    ItemSpacing="0"
                    Orientation="Vertical"
                    SnapPointsAlignment="Start"
                    SnapPointsType="MandatorySingle" />
            </CarouselView.ItemsLayout>

            <CarouselView.ItemTemplate>
                <grial:IntMemberTemplateSelector MemberName="Position">
                    <grial:IntMemberTemplateSelector.Items>

                        <!--  CAROUSEL'S PAGE 0  -->
                        <grial:IntMemberTemplateSelectorItem Value="0">
                            <DataTemplate>
                               
                               .. more elements omitted
                               
                            </DataTemplate>
                        </grial:IntMemberTemplateSelectorItem>

                        <!--  CAROUSEL'S PAGE 1  -->
                        <grial:IntMemberTemplateSelectorItem Value="1">
                            <DataTemplate>
                                
                                .. more elements omitted                                
                                      
                            </DataTemplate>
                        </grial:IntMemberTemplateSelectorItem>

                    </grial:IntMemberTemplateSelector.Items>
                </grial:IntMemberTemplateSelector>
            </CarouselView.ItemTemplate>

        </CarouselView>

    </ContentView.Content>
</ContentView>

视图模型

namespace PCS2.APP.viewmodels
{
    public class ActivityOverviewviewmodel : ObservableObject
    {
        private List<ActivityLocation> activityData;
        private readonly IRoutingService _routingService;
        private double _screenopacity;
        private bool _showLoadingAnimation;
        private int? _clientId;
        private int _position;
        private int _innerCarouselPosition;

        // Position of the Parent page CarouselView
        public int Position
        {
            get { return _position; }
            set { SetProperty(ref _position,value); }
        }

        // Data source for the child data
        public List<ActivityLocation> ActivityData
        {
            get { return activityData; }
            set { SetProperty(ref activityData,value); }
        }

    
        public double Screenopacity
        {
            get { return _screenopacity; }
            set { SetProperty(ref _screenopacity,value); }
        }

        public bool ShowLoadingAnimation
        {
            get { return _showLoadingAnimation; }
            set { SetProperty(ref _showLoadingAnimation,value); }
        }

        public ActivityOverviewviewmodel(int? clientId = null,IRoutingService routingService = null)
            : base(listenCultureChanges: true)
        {
            _clientId = clientId;
            _routingService = routingService ?? Locator.Current.GetService<IRoutingService>();
            LoadData();
        }

        private async void LoadData()
        {
            try
            {
                ShowLoadingAnimation = true;
                Screenopacity = 0.1;

                // Getting the data
                var _activitiesData = await App.Database.GetActivityDataAsync(_clientId,DateTime.UtcNow);
                ActivityData = _activitiesData;

            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                ShowLoadingAnimation = false;
                Screenopacity = 1.0;
            }

        }


    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?