如何在NavigationViewItem中更改字体颜色(前景)?

时间:2018-12-29 22:42:50

标签: uwp uwp-xaml

您将看到我正在创建UWP应用,并且我正在使用(NavigationView)作为主菜单,在NavigationViewItem中,我设法更改了背景,选择线等,但是我无法更改字体颜色< / p>

示例屏幕截图

https://i.imgur.com/Oc9ffxG.png

我的xaml代码:

 <NavigationView Grid.Row="1" x:Name="nvSample" PaneDisplayMode="Top" SelectionChanged="NvSample_SelectionChanged"
              IsSettingsVisible="True" IsBackButtonVisible="Collapsed">


        <NavigationView.MenuItems>
            <NavigationViewItem Content="Menu Item1" Tag="Home_Page" HorizontalAlignment="Stretch" Height="38" Margin="12,0,0,0" VerticalAlignment="Stretch"  />
            <NavigationViewItem Content="Instalpage" Tag="Install_Page" />
            <NavigationViewItem Content="Menu Item3" Tag="SamplePage3" />
            <NavigationViewItem Content="Menu Item4" Tag="SamplePage4" />


        </NavigationView.MenuItems>

        <Frame x:Name="contentFrame" >

        <Frame.ContentTransitions>
            <TransitionCollection>
                    <NavigationThemeTransition>
                        <DrillInNavigationTransitionInfo />
                    </NavigationThemeTransition>
            </TransitionCollection>
        </Frame.ContentTransitions>
         </Frame>

            <NavigationView.PaneFooter>

                <StackPanel Orientation="Horizontal">
                    <!--<NavigationViewItem Icon="SelectAll" Content="Select all" Foreground="White" />
                    <NavigationViewItem Icon="Help" Content="Help" />-->

                </StackPanel>

            </NavigationView.PaneFooter>

    </NavigationView>

2 个答案:

答案 0 :(得分:1)

  

但是我无法更改字体颜色

您可以使用TextBlock控件作为NavigationViewItem的内容,然后可以为TextBlock控件设置Foreground属性。

请参阅以下代码示例以供参考:

<NavigationView Grid.Row="1" x:Name="nvSample" PaneDisplayMode="Top"
          IsSettingsVisible="True" IsBackButtonVisible="Collapsed">
        <NavigationView.MenuItems>
            <NavigationViewItem Tag="Home_Page" HorizontalAlignment="Stretch" Height="38" Margin="12,0,0,0" VerticalAlignment="Stretch">
                <NavigationViewItem.Content>
                    <TextBlock Text="Menu Item1" Foreground="Red"></TextBlock>
                </NavigationViewItem.Content>
            </NavigationViewItem>
            <NavigationViewItem Content="Instalpage" Tag="Install_Page" />
            <NavigationViewItem Content="Menu Item3" Tag="SamplePage3" />
            <NavigationViewItem Content="Menu Item4" Tag="SamplePage4" />
        </NavigationView.MenuItems>
        <Frame x:Name="contentFrame" >

            <Frame.ContentTransitions>
                <TransitionCollection>
                    <NavigationThemeTransition>
                        <DrillInNavigationTransitionInfo />
                    </NavigationThemeTransition>
                </TransitionCollection>
            </Frame.ContentTransitions>
        </Frame>
        <NavigationView.PaneFooter>
            <StackPanel Orientation="Horizontal">
                <!--<NavigationViewItem Icon="SelectAll" Content="Select all" Foreground="White" />
                <NavigationViewItem Icon="Help" Content="Help" />-->
            </StackPanel>
        </NavigationView.PaneFooter>
    </NavigationView>

enter image description here

答案 1 :(得分:1)

NavigationViewItem确实有一个名为Foreground的属性,并且对我有用

 private void setAsWallpaper()
    {
        Picasso.get().load(imageUrl).into(new Target()
        {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
            {
                progressBar.bringToFront();
                progressBar.setVisibility(View.VISIBLE);

                WallpaperManager wallpaperManager = WallpaperManager.getInstance(SetWallPaperFullScreenActivity.this);
                try
                {
                    wallpaperManager.setBitmap(bitmap);

                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                Toast.makeText(SetWallPaperFullScreenActivity.this, "Wallpaper set successfully.", Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable)
            {
                Log.d("TAG", "Failed: ");

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable)
            {
                Log.d("TAG", "Prepare Load: ");

            }

        });

    }