如何解决从 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 举报,一经查实,本站将立刻删除。