如何从事件处理程序(在 Xaml.cs 代码中)与 CollectionView 中的视图交互

时间:2021-03-19 18:47:59

标签: c# xaml xamarin xamarin.forms mvvm

我正在尝试使用 MediaElement 视图的参数“Duration”的值更改标签的参数“text”,它们都是同一个父项(Grid)的子项,并且该 Grid 位于集合视图。我想在单击框架(也是同一个 Grid 父级的子级)时更改参数文本。

<Grid
                            HeightRequest="145"
                            ColumnDefinitions="25*,75*"
                            RowDefinitions="21*,21*,24*,34*"
                            Padding="0"
                            RowSpacing="0">
                            <!--Only plays programmatically once, why? No idea-->
                            <xct:MediaElement
                                x:Name="mediaa"
                                Source="{Binding messageVoice}"
                                IsLooping="True"
                                KeepScreenOn="True"
                                AutoPlay="False"
                                ShowsPlaybackControls="False"
                                
                                HeightRequest="0"
                                WidthRequest="0"
                                Margin="0,0,0,0"
                                VerticalOptions="Center"
                                HorizontalOptions="Center"
                                Grid.Column="0"
                                Grid.Row="3">
                            </xct:MediaElement>
                            <Frame
                                x:Name="PlayFrame"
                                HasShadow="False"
                                CornerRadius="20"
                                BackgroundColor="Transparent"
                                HeightRequest="40"
                                WidthRequest="40"
                                IsClippedToBounds="True"

                                VerticalOptions="Start"
                                HorizontalOptions="Center"
                                Padding="0"
                                Grid.Column="1"
                                Grid.Row="3"
                                Margin="0,0,0,0">
                                <Image
                                    Source="play.png"

                                    Aspect="AspectFill"
                                    HeightRequest="100">
                                </Image>
                                <Frame.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Tapped="Play_Tapped">
                                    </TapGestureRecognizer>
                                </Frame.GestureRecognizers>
                            </Frame>
<Label
                                Text="I want to change this property"
                                TextColor="Black"
                                FontAttributes="None"
                                FontSize="16"
                                    
                                Margin="15,0"
                                VerticalOptions="Start"
                                HorizontalOptions="Start"
                                Grid.Row="3"
                                Grid.Column="1">
                            </Label>
                        </Grid>

由于所有这些视图都位于 CollectionView 中,我无法使用它们的 x:names 访问它们,我能够从中提取信息的唯一方法是执行 TapGestureRecognizer 并访问这些视图的副本,但这样做我只能提取信息,不能改变它:

void Play_Tapped(System.Object sender, System.EventArgs e)
        {
            Frame frame = (Frame)sender;
            Grid parent = (Grid)frame.Parent;
            var audio = (MediaElement)parent.Children[0];
            audio.Play();
//I should change the text property here, but I couldn't do that since these views are just copies of the originals
        }

我知道我可以将属性绑定到不同视图的属性,但是由于属性持续时间是 TimeSpan?属性,文本是字符串属性,我必须在更改文本值之前进行适当的转换。

知道如何从后面的代码 (xaml.cs) 访问这些属性吗?

感谢您抽出宝贵时间,如果您需要更多信息,我会在看到您的请求后立即提供给您。

0 个答案:

没有答案
相关问题