相对布局中元素上的事件未触发

时间:2017-06-27 16:13:16

标签: button xamarin.forms relativelayout

我正在使用Xamarin Forms并且相对布局内的元素上的事件未触发。我试图在后面的代码中注册它们,但仍然没有解雇。我有-250约束因为按钮显示在屏幕的底部,这是我可以移动它们的唯一方法。 这是我的xaml:

 <StackLayout Orientation="Vertical">
    <RelativeLayout>
        <Grid Margin="0,0,10,0"
              x:Name="grid"
              RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=10}" 
              RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=0, Constant=0}" 
              >


        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50*" />
            <ColumnDefinition Width="50*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0"
               Grid.Row="0"
                Text="IP Address"
               Margin="10,10,0,0"
               HorizontalOptions="StartAndExpand"/>
        <Entry Grid.Column="1"  
               Grid.Row="0"
            x:Name="IPAddress"
            Placeholder="IP"
               WidthRequest="200"

                Margin="0,0,10,0"
            HorizontalOptions="EndAndExpand"/>
        <Label Grid.Column="0"
               Grid.Row="1"
                Text="Port"
               Margin="10,10,0,0"
               MinimumWidthRequest="100"
               HorizontalOptions="Start"/>
        <Entry Grid.Column="1"  
               Grid.Row="1"
            x:Name="Port"
               Margin="0,0,10,0"
            Placeholder="Port"
                WidthRequest="200"
            HorizontalOptions="End"/>
        <Label Grid.Column="0"
               Grid.Row="2"
               Margin="10,10,0,0"
                Text="User Name"
               MinimumWidthRequest="100"
               HorizontalOptions="Start"/>
        <Entry Grid.Column="1"  
               Grid.Row="2"
            x:Name="UserName"
               Margin="0,0,10,0"
                WidthRequest="200"
            Placeholder="User Name"
            HorizontalOptions="End"/>
        <Label Grid.Column="0"
               Grid.Row="3"
               Margin="10,10,0,0"
                Text="Password"
               MinimumWidthRequest="100"
               HorizontalOptions="Start"/>
        <Entry Grid.Column="1"  
               Grid.Row="3"
            x:Name="Password"
               IsPassword="True"
               Margin="0,0,10,0"
                WidthRequest="200"
            Placeholder="Password"
            HorizontalOptions="End"/>

        </Grid>

    </RelativeLayout>
    <RelativeLayout
        IsEnabled="True">
        <Button x:Name="SaveBtn"
                IsEnabled="True"
                WidthRequest="100"
                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=-250}"
                RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=0, Constant=30}"
                Clicked="SaveBtn_Clicked"
                BorderColor="Gray"
                Text="Save"></Button>
        <Button x:Name="ClearFavorites"
                IsEnabled="True"
                WidthRequest="120"
                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=-250}" 
                RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5, Constant=20}"
                Clicked="ClearFavorites_Clicked"
                BorderColor="Gray"
                Text="Clear Favorites"></Button>
    </RelativeLayout>
</StackLayout>

1 个答案:

答案 0 :(得分:1)

在讨论聊天后,这似乎有效

<StackLayout Orientation="Vertical"> 
<RelativeLayout> 
<Grid Margin="0,0,10,0" 
x:Name="grid" 
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=10}" 
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=0, Constant=0}" 
> 


<Grid.RowDefinitions> 
<RowDefinition Height="Auto"/> 
</Grid.RowDefinitions> 
<Grid.ColumnDefinitions> 
<ColumnDefinition Width="50*" /> 
<ColumnDefinition Width="50*" /> 
</Grid.ColumnDefinitions> 

<Label Grid.Column="0" 
Grid.Row="0" 
Text="IP Address" 
Margin="10,10,0,0" 
HorizontalOptions="StartAndExpand"/> 
<Entry Grid.Column="1" 
Grid.Row="0" 
x:Name="IPAddress" 
Placeholder="IP" 
WidthRequest="200" 

Margin="0,0,10,0" 
HorizontalOptions="EndAndExpand"/> 
<Label Grid.Column="0" 
Grid.Row="1" 
Text="Port" 
Margin="10,10,0,0" 
MinimumWidthRequest="100" 
HorizontalOptions="Start"/> 
<Entry Grid.Column="1" 
Grid.Row="1" 
x:Name="Port" 
Margin="0,0,10,0" 
Placeholder="Port" 
WidthRequest="200" 
HorizontalOptions="End"/> 
<Label Grid.Column="0" 
Grid.Row="2" 
Margin="10,10,0,0" 
Text="User Name" 
MinimumWidthRequest="100" 
HorizontalOptions="Start"/> 
<Entry Grid.Column="1" 
Grid.Row="2" 
x:Name="UserName" 
Margin="0,0,10,0" 
WidthRequest="200" 
Placeholder="User Name" 
HorizontalOptions="End"/> 
<Label Grid.Column="0" 
Grid.Row="3" 
Margin="10,10,0,0" 
Text="Password" 
MinimumWidthRequest="100" 
HorizontalOptions="Start"/> 
<Entry Grid.Column="1" 
Grid.Row="3" 
x:Name="Password" 
IsPassword="True" 
Margin="0,0,10,0" 
WidthRequest="200" 
Placeholder="Password" 
HorizontalOptions="End"/> 

</Grid> 

</RelativeLayout> 
<RelativeLayout 
IsEnabled="True"> 
<Button x:Name="SaveBtn" 
IsEnabled="True" 
WidthRequest="100" 
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=50}" 
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=0, Constant=30}" 
Clicked="SaveBtn_Clicked" 
BorderColor="Gray" 
Text="Save"></Button> 
<Button x:Name="ClearFavorites" 
IsEnabled="True" 
WidthRequest="120" 
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=50}" 
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5, Constant=20}" 
Clicked="ClearFavorites_Clicked" 
BorderColor="Gray" 
Text="Clear Favorites"></Button> 
</RelativeLayout> 
</StackLayout>