我需要在父级xaml中执行对子级xaml的回调响应,并在那里做一些事情
儿童xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="project.NavigationBar">
<ContentView.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackLayout
Orientation="Horizontal"
Grid.Column="0"
HorizontalOptions="CenterAndExpand"
VerticalOptions="End">
<Button
x:Name="NavigationBarButton"
Clicked="NavigationBarButton_Clicked"
Text="Gallery">
</Button>
</StackLayout>
</Grid>
</ContentView.Content>
</ContentView>
孩子xaml.cs:
private void NavigationBarButton_Clicked(object sender, EventArgs e)
{
navigationBarButton_Clicked?.Invoke(sender, e);
}
public void logicFunc(params)
{
do stuff there
}
父母xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="project.main"
xmlns:views="project.NavigationBar"
>
<views:NavigationBar
navigationBarButton_Clicked="NavigationBarButton_Clicked">
</views:NavigationBar>
</ContentPage>
父项xaml.cs:
private void NavigationBarButton_Clicked(object sender, EventArgs e)
{
NavigationBar navbar = new NavigationBar();
navbar.logicFunc(params);
}
我如何调用子级xaml的父级响应表?
答案 0 :(得分:1)
在父项的xaml中为子元素设置一个已定义的名称:
<views:NavigationBar x:Name="NavBar"
navigationBarButton_Clicked="NavigationBarButton_Clicked">
</views:NavigationBar>
然后在事件处理程序调用的方法(在父视图中)中,您可以通过元素的名称访问元素并调用公共方法:
private void NavigationBarButton_Clicked(object sender, EventArgs e)
{
NavBar.logicFunc(params);
}