在uwp中的fontIcon上添加一个下拉菜单

时间:2016-03-27 18:49:23

标签: xaml winrt-xaml win-universal-app windows-10-universal

我目前正在开发一个具有自定义标题栏的项目,该标题栏是使用https://marcominerva.wordpress.com/2015/05/19/easily-manage-the-title-bar-in-windows-10-apps/中的示例创建的。使用该示例,我能够创建类似于http://i.stack.imgur.com/RzSFr.png的菜单。到目前为止,自定义标题条形码看起来像这样

<Border x:Name="customTitleBar" VerticalAlignment="Top" Height="32"   Background="Transparent" FlyoutBase.AttachedFlyout="{StaticResource FlyoutBase1}">
        <StackPanel Margin="12,5,5,5" Orientation="Horizontal">
            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE700;"
                      Foreground="Black" VerticalAlignment="Center" Margin="12,0,8,0">

            </FontIcon>
            <TextBlock Text="My app" Foreground="Black"
                       VerticalAlignment="Center" Margin="25,0"/>
        </StackPanel>

        <i:Interaction.Behaviors>
            <local:TitleBarBehavior IsChromeless="True"/>
        </i:Interaction.Behaviors>
    </Border>

注意:汉堡图标已插入上面的fontIcon。与上图类似,我希望在下拉列表中有一个共享和设置命令。我仍然是Windows 10 uwp的新手,有没有办法将FontIcom包装在MenuFlyout控件中,我知道这听起来不对吗?我还尝试在XAML中更改PointerEntered上的fontIcon的颜色,如何在不在后面的代码中放置事件的定义的情况下实现此目的?

2 个答案:

答案 0 :(得分:1)

<强> &GT;&GT;有没有办法将FontIcom包装在MenuFlyout控件

你的意思是你想在MenuFlyout中添加FontIcon,就像我用红色突出显示的一样吗?

enter image description here

如果是这样,最好使用Flyout而不是MenuFlyout,因为默认情况下,MenuFlyout会有许多具有Text属性的MenuFlyoutItems(字符串值),这样我们就无法添加控件等作为MenuFlyout中的FontIcon。

有关如何使用Flyout来实现您的要求,请尝试参考:

  <Flyout>
         <StackPanel Orientation="Vertical">
             <StackPanel Orientation="Horizontal">
                        <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE72D;" Foreground="Black"/>
                        <TextBlock Text="Share"></TextBlock>
              </StackPanel>
              <StackPanel Orientation="Horizontal">
                   <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE713;" Foreground="Black"/>
                   <TextBlock Text="Settings"></TextBlock>
              </StackPanel>
          </StackPanel>
</Flyout>

结果:

enter image description here

<强> &GT;&GT;我还尝试在XAML中更改PointerEntered上的fontIcon的颜色,如何在不在后面的代码中定义事件的情况下实现此目的?

对于这个问题,请在另一个帖子中查看我的回复:

Simulating a mousehover effect on a fontIcon in uwp

感谢。

答案 1 :(得分:1)

搜索互联网并阅读Flyout菜单后。 Fang回答抛出错误&#34; Flyout类型的值无法添加到集合或UIElementCollection元素中。弹出菜单只能根据https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.flyout.aspx添加到button.flyout属性中。

我已经改进了方回答来解决我的问题。重新实现如下所示

<Button FontFamily="Segoe MDL2 Assets" Content="&#xE700;"
                      Foreground="Black" VerticalAlignment="Center" Margin="12,0,8,0" Background="Transparent">
                <Button.Flyout>
                <Flyout>
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE72D;" Foreground="Black"/>
                            <TextBlock Text="Share" Margin="5,0"></TextBlock>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <FontIcon Margin="0,5" FontFamily="Segoe MDL2 Assets" Glyph="&#xE713;" Foreground="Black"/>
                                <TextBlock Text="Settings" Margin="5,5"></TextBlock>
                        </StackPanel>
                    </StackPanel>
                </Flyout>
                </Button.Flyout>
            </Button>