行为OnAttachedTo事件未触发

时间:2018-03-18 18:12:06

标签: xaml xamarin xamarin.forms

我的行为设置使用以下代码(这只是一个代码段):

public class StarBehaviour : Behavior<View>
{
    TapGestureRecognizer tapRecognizer;
    protected override void OnAttachedTo(View view)
    {
        tapRecognizer = new TapGestureRecognizer();
        tapRecognizer.Tapped += OnTapRecognizerTapped;
        view.GestureRecognizers.Add(tapRecognizer);
    }

    protected override void OnDetachingFrom(View view)
    {
        view.GestureRecognizers.Remove(tapRecognizer);
        tapRecognizer.Tapped -= OnTapRecognizerTapped;
    }

    void OnTapRecognizerTapped(object sender, EventArgs args)
    {
        //HACK: PropertyChange does not fire, if the value is not changed :-(
        IsStarred = false;
        IsStarred = true;
    }

我创建了一个水平堆栈布局,将每个StarBehaviour存储在它自己的网格中。问题是当我在我的设备上测试时,无法识别水龙头。我觉得它可能与视图有关,但当我在背景颜色上添加背景颜色时,它似乎相对正常。以下是实施StarBehaviour的xaml:

<?xml version="1.0" encoding="utf-8" ?> <TabbedPage BackgroundColor="#8BC739" x:Name="MainTabbedPage" xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:SalApp;"
         x:Class="SalApp.pages.RecipePage">
<!--Pages can be added as references or inline-->
<ContentPage x:Name="AboutContentPage" Title="About" BackgroundColor="LightGray">
    <ScrollView>
        <StackLayout Orientation="Vertical" x:Name="AboutStack">
            <BoxView x:Name ="imageBoxView" Color="DarkOrange" HorizontalOptions="FillAndExpand" BindingContext="{x:Reference imageBoxView}" 
      HeightRequest="{Binding Width}" />
            <Grid x:Name="textAndRatingGrid" Margin="16,4,0,0" HeightRequest="20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <Label BindingContext="{x:Reference MainTabbedPage}" Text="{Binding Title}" Grid.Column="0"/>
                <StackLayout Margin="32,0,4,0" Grid.Column="1" Orientation="Horizontal" HorizontalOptions="Fill">
                    <Grid BackgroundColor="DarkBlue">
                        <Grid.Behaviors>
                            <local:StarBehaviour x:Name="starOne" GroupName="myStar"/>
                        </Grid.Behaviors>
                        <Image Aspect="AspectFill" x:Name="starBlankOne" Source="stars/star_empty.png"/>
                        <Image Aspect="AspectFill" x:Name="starSelectedOne" Source="stars/star_full.png" IsVisible="{Binding Source={x:Reference starOne}, Path=IsStarred}"/>
                    </Grid>
                    <Grid>
                        <Grid.Behaviors>
                            <local:StarBehaviour x:Name="starTwo" GroupName="myStar"/>
                        </Grid.Behaviors>
                        <Image x:Name="starBlankTwo" Source="stars/star_empty.png"/>
                        <Image x:Name="starSelectedTwo" Source="stars/star_full.png" IsVisible="{Binding Source={x:Reference starTwo}, Path=IsStarred}"/>
                    </Grid>
                    <Grid>
                        <Grid.Behaviors>
                            <local:StarBehaviour x:Name="starThree" GroupName="myStar"/>
                        </Grid.Behaviors>
                        <Image x:Name="starBlankThree" Source="stars/star_empty.png"/>
                        <Image x:Name="starSelectedThree" Source="stars/star_full.png" IsVisible="{Binding Source={x:Reference starThree}, Path=IsStarred}"/>
                    </Grid>
                    <Grid>
                        <Grid.Behaviors>
                            <local:StarBehaviour x:Name="starFour" GroupName="myStar"/>
                        </Grid.Behaviors>
                        <Image x:Name="starBlankFour" Source="stars/star_empty.png"/>
                        <Image x:Name="starSelectedFour" Source="stars/star_full.png" IsVisible="{Binding Source={x:Reference starFour}, Path=IsStarred}"/>
                    </Grid>
                    <Grid>
                        <Grid.Behaviors>
                            <local:StarBehaviour x:Name="starFive" GroupName="myStar"/>
                        </Grid.Behaviors>
                        <Image x:Name="starBlankFive" Source="stars/star_empty.png"/>
                        <Image x:Name="starSelectedFive" Source="stars/star_full.png" IsVisible="{Binding Source={x:Reference starFive}, Path=IsStarred}"/>
                    </Grid>
                </StackLayout>
            </Grid>
            <StackLayout Margin ="16,4" Orientation="Vertical">
                <Label FontAttributes="Bold" Text="Description"/>
                <Label HorizontalOptions="Fill" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non felis vitae ligula pretium congue. Integer mollis, mauris id pretium vulputate, lacus nibh pulvinar eros, euismod eleifend elit felis non ipsum. Morbi at metus quis quam pellentesque tempus ac pellentesque orci. Quisque rhoncus vulputate fringilla. Suspendisse erat arcu, pulvinar ut tincidunt non, euismod in elit. In rhoncus nibh massa, vel blandit dolor sollicitudin id. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur sollicitudin rhoncus quam quis sagittis. Fusce pharetra pretium finibus. Aliquam iaculis eget nisi laoreet vehicula. Donec odio ligula, iaculis id mollis nec, ultricies eu magna. Suspendisse molestie, erat ac gravida vulputate, neque elit ultricies leo, ut tristique mauris est eget dolor."/>
            </StackLayout>
        </StackLayout>
    </ScrollView>
</ContentPage>
<ContentPage Title="Recipe" BackgroundColor="LightGray"/>
<ContentPage Title="Ingredients" BackgroundColor="LightGray"/>
</TabbedPage> 

任何想法都可能出错?

更新:我知道它与xaml上的绑定有关。可能是我设置绑定的方式意味着它不绑定到实际视图。由于我在标签页中构建内容页面,这似乎是可能的。虽然我是Xamarin绑定的初学者,所以我不能自己选择这个。

1 个答案:

答案 0 :(得分:0)

根据你分享的片段。您正试图听取Grid内每个StackLayout的点击操作。网格在StackLayout内的放置部分没有任何错误。唯一的事情是你把Image放在Grid中。所以有一个放置在网格上方的视图。触摸的第一优先级将用于图像视图。尝试将InputTransparent的{​​{1}}属性设置为true。因此,触摸可以传递给网格,ImageView回调将被触发。