我在Maui项目中遇到XAML问题。免责声明:我不是这方面的Maven,所以请你饶了我吧;).
这是我的xaml:
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ApeEvoPrototype.NumPad">
<ScrollView>
<Grid
ColumnDefinitions="{DynamicResource NumPad_GridColumnPrincipale}"
RowDefinitions="{DynamicResource NumPad_GridRowPrincipale}">
<Frame
Grid.Row="0"
Grid.Column="0"
Style="{DynamicResource NumPad_Frame}">
<Grid x:Name="numPadGrid" Style="{DynamicResource NumPad_GridInterno}">
<StackLayout Grid.Row="0" Grid.ColumnSpan="3">
<Grid x:Name="containerEditor" Style="{DynamicResource NumPad_GridLinguetta}">
<Label Grid.Column="0" VerticalTextAlignment="Center" Text="Quantity:" TextColor="Black" />
<Editor Grid.Column="1" x:Name="txtQuantity" VerticalTextAlignment="Center" HorizontalTextAlignment="End"/>
<Button Grid.Column="2" x:Name="btnBck" Text="Bck" Clicked="Button_Clicked_Bck" BindingContext="2000" />
</Grid>
</StackLayout>
<StackLayout Grid.Column="0" Grid.Row="1" Style="{DynamicResource NumPad_StackTastierino}">
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn1" Style="{DynamicResource NumPad_Button}" Text="1" Clicked="Button_Clicked" BindingContext="1"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn4" Style="{DynamicResource NumPad_Button}" Text="4" Clicked="Button_Clicked" BindingContext="4"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn7" Style="{DynamicResource NumPad_Button}" Text="7" Clicked="Button_Clicked" BindingContext="7"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btnTot" Style="{DynamicResource NumPad_Button}" Text="All" Clicked="Button_Clicked_Tot" BindingContext="3000" />
</Grid>
</StackLayout>
<StackLayout Grid.Column="1" Grid.Row="1" Style="{DynamicResource NumPad_StackTastierino}">
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn2" Style="{DynamicResource NumPad_Button}" Text="2" Clicked="Button_Clicked" BindingContext="2"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn5" Style="{DynamicResource NumPad_Button}" Text="5" Clicked="Button_Clicked" BindingContext="5"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn8" Style="{DynamicResource NumPad_Button}" Text="8" Clicked="Button_Clicked" BindingContext="8"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn0" Style="{DynamicResource NumPad_Button}" Text="0" Clicked="Button_Clicked" BindingContext="0"/>
</Grid>
</StackLayout>
<StackLayout Grid.Column="2" Grid.Row="1" Style="{DynamicResource NumPad_StackTastierino}">
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn3" Style="{DynamicResource NumPad_Button}" Text="3" Clicked="Button_Clicked" BindingContext="3"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn6" Style="{DynamicResource NumPad_Button}" Text="6" Clicked="Button_Clicked" BindingContext="6" />
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btn9" Style="{DynamicResource NumPad_Button}" Text="9" Clicked="Button_Clicked" BindingContext="9"/>
</Grid>
<Grid Style="{DynamicResource NumPad_GridTastierino}">
<Button x:Name="btnC" Style="{DynamicResource NumPad_Button}" Text="C" Clicked="Button_Clicked_C" BindingContext="1000" />
</Grid>
</StackLayout>
</Grid>
</Frame>
</Grid>
</ScrollView>
</ContentView>
正如您所看到的,我有很多名为“btn?"的按钮,我的目标是动态访问这些按钮,而不使用它们的名称。唯一的方法是定义包含按钮的单个网格的名称,就像我在这里所做的那样:
<Grid x:Name="containerEditor" Style="{DynamicResource NumPad_GridLinguetta}">
并在我代码中执行:
var buttons = containerEditor.Children.OfType<Button>();
这样我就可以访问Grid中的所有资源按钮。这种方法的问题是Children
方法只探索控件的第一层,它不能递归地工作。
有人能帮帮我吗?
2条答案
按热度按时间xwbd5t1u1#
像这样的方法应该可以。
将您的根命名为“Root”(在此示例中),并按如下方式使用:
xeufq47z2#
我通过将所有控件处理到视图中来解决List问题