[UWP]如何更改应用程序标题栏的文本?

人气:716 发布:2022-09-22 标签: wpdevelop

问题描述

我想更改应用程序标题栏的文本。目前它的"AppName"是我们设置清单。我想在栏中显示:

I want to change the text of the application Title bar. Currently its the "AppName" we set in manifest. I want to show this in bar:

AppName - RandomTextWhichMayChangeRunTime

当我使用 时;

When I am do using

ApplicationView appView = ApplicationView.GetForCurrentView();
appView.Title = "RandomTextWhichMayChangeRunTime";

它显示:

RandomTextWhichMayChangeRunTime - AppName

这是我实际需要的交换版本。应用程序名称会自动添加,但最后,我需要在开头添加。

It is the swapped version which I need actually. App name is appended automatically but at the end, I need it in the beginning.

照片应用程序执行此操作。它显示了"Photos - Imagename.jpg"。在顶部。我想以同样的方式做到这一点。

Photos app does it. It shows the "Photos - Imagename.jpg" at the top. I want to do it in the same way.

然后我搜索了更多并在App.xaml.cs中尝试了这个OnLaunched:

Then I searched more and tried this in App.xaml.cs OnLaunched:

var coreTitleBar = CoreApplication.GetCurrentView().TitleBar; coreTitleBar.ExtendViewIntoTitleBar = true; AppTitleBar appbar = new AppTitleBar();

Window.Current.SetTitleBar(appbar);

AppTitleBar是一个像这样定义的单独页面:

AppTitleBar is a separate page defined like this:

 <Grid x:Name="AppTitleBar1" Background="Transparent" Height="40" Width="100" HorizontalAlignment="Left" VerticalAlignment="Center">

        <TextBlock x:Name="NameText" Text="Hello bro!" Foreground="Black"
               Margin="44,8,0,0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
    </Grid>

但它只是删除了带有白色屏幕的默认顶栏而没有别的。

But it just removes the default top bar with white screen and nothing else.

你能建议我如何实现它吗?

Can you suggest how I can achieve it?

谢谢。

推荐答案

您可以自定义标题栏以匹配应用的个性。标题栏自定义API允许您指定标题栏元素(按钮,文本)的颜色,或将应用程序内容扩展到标题栏区域并完全控制它。请参考到 文档。

You can customize the title bar to match the personality of your app. The title bar customization APIs let you specify colors for title bar elements(buttons, text), or extend your app content into the title bar area and take full control of it. Please refer to this document.

对于此问题,您可以通过设置 的 CoreApplicationViewTitleBar.ExtendViewIntoTitleBar 属性为true, Window.Current.SetTitleBar 无法显示单独页面或UserControl的自定义标题甚至UIElement也是动态实例化的。您可以在不更改的根页面中声明和设置标题栏,也可以在应用程序可以导航到的每个页面中声明和设置标题栏区域,但不能设置标题栏在应用程序OnLauched方法。请参阅以下代码。

For this issue, you opt for full customization via setting CoreApplicationViewTitleBar.ExtendViewIntoTitleBar property to true, Window.Current.SetTitleBar could not show the custom title for a separate page or UserControl, even the UIElement is instanced dynamically. You can either declare and set the title bar in a root page that doesn’t change, or declare and set a title bar region in each page that your app can navigate to, but you can’t set the title bar in app OnLauched method. Please see following code.

App.xaml.cs

App.xaml.cs

  protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            . . .

            // Hide default title bar.
            var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
            coreTitleBar.ExtendViewIntoTitleBar = true;
        }

MainPage.Xaml

MainPage.Xaml

    <Grid x:Name="AppTitleBar" Background="Transparent">
        <!-- Width of the padding columns is set in LayoutMetricsChanged handler. -->
        <!-- Using padding columns instead of Margin ensures that the background
         paints the area under the caption control buttons (for transparent buttons). -->
        <TextBlock Text="Custom Title Bar" 
                   x:Name="TextAppTitle"
               Style="{StaticResource CaptionTextBlockStyle}" 
               Margin="12,8,0,0"/>
    </Grid>

MainPage.Xaml.cs

MainPage.Xaml.cs

        public MainPage()
        {
            this.InitializeComponent();

            Package package = Package.Current;
            var appName = package.DisplayName;
            TextAppTitle.Text =

" {appName} - RandomTextWhichMayChangeRunTime" ;; Window.Current.SetTitleBar(AppTitleBar); } "{appName} - RandomTextWhichMayChangeRunTime"; Window.Current.SetTitleBar(AppTitleBar); }

祝你好运,

Roy

725