5

恐怕我在这里遗漏了一些简单的东西,但我试图在我的应用栏中放置多个按钮。

这是初始应用栏的代码

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left">
        <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
    </AppBar>
</common:LayoutAwarePage.TopAppBar>

现在这段代码有效,但我想添加第二个按钮。

我认为这很容易做到,我尝试:

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left"> <!-- line 19 -->
        <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
        <Button  x:Name="newButton" Content="NewButton" />  <!-- line 21 -->
    </AppBar>
</common:LayoutAwarePage.TopAppBar>

但我收到以下错误:

The property "Content" can only be set once. (line 19)
The property "Content" can only be set once. (line 21)
Duplication assignment to the 'Content' property of the 'AppBar' object (line 21)

我认为可能有一些AppBar我缺少的控件。我如何在其中放置第二个按钮?

4

2 回答 2

5

简单地把所有东西都放在一个StackPanelinAppBar

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left"> <!-- line 19 -->
        <StackPanel Orientation="Horizontal">
            <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
            <Button  x:Name="newButton" Content="NewButton" />  <!-- line 21 -->
        </StackPanel>
    </AppBar>
</common:LayoutAwarePage.TopAppBar>

为了更好地控制,您可以使用 aGrid这将允许您在双方都有内容:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <StackPanel x:Name="LeftPanel" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
        <!-- Left Content here -->
    </StackPanel>
    <StackPanel x:Name="RightPanel" Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right">
        <!-- Right Content here -->
    </StackPanel>
</Grid>
于 2012-11-16T17:32:49.627 回答
3

您正在设置内容两次,这正是错误所说的。相反,将内容设置为可以接受多个子项的内容,例如“StackPanel”,如下所示:

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left"> <!-- line 19 -->
        <StackPanel Orientation="Horizontal">
            <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
            <Button  x:Name="newButton" Content="NewButton" />  <!-- line 21 -->
        </StackPanel>
    </AppBar>
</common:LayoutAwarePage.TopAppBar>
于 2012-11-16T17:33:26.550 回答