【问题标题】:multiple buttons in appbar应用栏中的多个按钮
【发布时间】:2012-11-16 17:30:25
【问题描述】:

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

这是初始应用栏的代码

<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 控件的某些内容。我如何在其中放置第二个按钮?

【问题讨论】:

    标签: c# xaml windows-8


    【解决方案1】:

    只需将 StackPanel 中的所有内容放在 AppBar

    <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>
    

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

    <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>
    

    【讨论】:

    • 我将在 6 分钟内接受您的回答。必须等待 15
    • 如果采用这种方法,则无需输入"50*",而"*" 将是相同的。此外 - 我建议放置一侧 "*" 和另一侧 "Auto" 取决于您主要使用哪一侧,以便在需要时适应使用一半以上的空间。跨度>
    • 感谢您的意见,我仍在学习如何使用 XAML,结果“看起来像预期的那样”:)
    • 它有效。我不是说你错了,只是提供一些见解/建议。欢迎!
    • 这正是这个网站以这种格式存在的原因,感谢您提供的信息,我相应地更新了代码
    【解决方案2】:

    您正在设置内容两次,这正是错误所说的。相反,将内容设置为可以接受多个子项的内容,例如“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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      相关资源
      最近更新 更多