Xamarin CarouselView到达子元素

时间:2018-07-11 13:22:16

标签: .net xaml xamarin xamarin.forms carousel

我最近开始开发Xamarin应用程序,但有一个问题,到目前为止我找不到出路。 在我的应用程序中,我有一个DTO:

 public class StoryContentDTO
{
    public int Id { get; set; }
    public string PictureUrl { get; set; }
    public bool IsDefault { get; set; }
    public bool IsActive { get; set; }
    public bool IsImage { get; set; }
    public int DisplayOrder { get; set; }
    public int AudioStartTime { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public bool _IsImage
    {
        get
        {

            if (this.PictureUrl.Contains("mp4"))
                return false;
            return true;



        }
    }
    public bool _IsVideo
    {
        get
        {

            if (this.PictureUrl.Contains("mp4"))
                return true;
            return false;



        }
    }
}

故事内容可以是图片或视频,在xaml方面,我所做的是:

<RelativeLayout x:Name="mainContainer" VerticalOptions="FillAndExpand"
                                       HorizontalOptions="FillAndExpand"
                                       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                                       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=1}">


    <Image                                       x:Name="playImage"
                                                 Source="play.png"
                                                 VerticalOptions="Start"
                                                 HorizontalOptions="Start"
                                                 Aspect="AspectFill"

                                                 RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.10 }"
                                                 RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=.10}"
                                                 RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0}"
                                                 RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0}"
                                                 IsVisible="{Binding IsPlaying}">

        <Image.GestureRecognizers x:Uid="audioButton">
            <TapGestureRecognizer

                                              NumberOfTapsRequired="1"

                                              Command="{Binding PlayPauseCommand}"  x:Name="myRecognizer"  />
        </Image.GestureRecognizers>


    </Image>

    <StackLayout x:Name="ExportNumber" HorizontalOptions="FillAndExpand"
                                       VerticalOptions="FillAndExpand"
                                       Padding="0" Margin="0" Spacing="0"
                                       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                                       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=1}">

        <cv:CarouselViewControl x:Name="images" PositionSelected="OnPositionSelected"
                                      Position="{Binding Position}"
                                      HorizontalOptions="FillAndExpand"
                                      VerticalOptions="FillAndExpand" Margin="0"
                                      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                                      RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=1}">

            <cv:CarouselViewControl.ItemTemplate>
                <DataTemplate>
                        <StackLayout  x:Name="containerLayout" Focused="OnFocused"
                                      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                                      RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=1}" BackgroundColor="Red">


                            <Image Source="{Binding PictureUrl}"
                                        Aspect="Fill" IsVisible="{Binding _IsImage}"
                                        BackgroundColor="Purple"
                                        RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.9}"
                                        RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=.9}"></Image>
                            <StackLayout x:Name="videoContainer">
                                <videoPlayer:VideoPlayer x:Name="{Binding Id}"
                                 HeightRequest="300"
                                 VideoSource="{Binding PictureUrl}"
                                 WidthRequest="600" IsVisible="True" AreControlsDisplayed="True" AutoPlay="False" MediaStateChanged="MediaState_Changed"/>
                            </StackLayout>

                            <!--<o:VideoPlayer  Source="{Binding PictureUrl}" IsVisible="{Binding _IsVideo}"  AutoPlay="True" DisplayControls="False" Completed="VideoPlayer_Completed"/>-->
                        </StackLayout>
                    </ViewCell>

                </DataTemplate>
            </cv:CarouselViewControl.ItemTemplate>
        </cv:CarouselViewControl>
    </StackLayout>
</RelativeLayout>

所以,我要区分视频和图片,以便仅当 StoryContentDTO 是带有 .mp4的视频时,才可以开始播放视频。 strong>扩展名,但是我找不到任何访问CarouselView的子元素的方法。有谁知道我如何在CarouselView中观看视频并检查它是否真的是视频

谢谢。

0 个答案:

没有答案