【问题标题】:Windows Universal App Fullscreen ButtonWindows 通用应用全屏按钮
【发布时间】:2016-03-21 12:51:41
【问题描述】:

Windows 应用商店中的某些应用在标题栏中的最小化、最大化和关闭按钮之外还有一个全屏按钮。如果全屏处于活动状态,此按钮看起来类似于每个应用程序在标题栏中的退出全屏按钮。这是一个系统控件吗?我如何在我的 C# 通用应用程序中使用它?

【问题讨论】:

  • 提供的任何答案是否可以解决您的问题?
  • 看看that

标签: c# windows-10 win-universal-app uwp


【解决方案1】:

您必须使用Window.SetTitleBar 方法来实现您想要的行为。因此,您需要完成几个步骤:

首先,让视图扩展到标题栏。请注意,您只能设置标题栏的左侧部分。 最小化、最大化和关闭按钮仍然存在:

CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;

设置好之后,调用Window.SetTitleBar 方法并使用UIElement:

Window.Current.SetTitleBar(myTitleBar);

myTitleBar 可能看起来像这样:

<Border x:Name="myTitleBar">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <!-- Title -->
        <TextBlock Grid.Column="0"
                   Text="..."/>

        <!-- Custom buttons attached to the right side -->
        <StackPanel Grid.Column="1"
                    Orientation="Horizontal">
            <Button x:Name="FullScreenButton"/>
            <!-- Use U+E740 FullScreen Icon for the button above -->
        </StackPanel>
    </Grid>
</Border

可以在 here 找到 Marco Minerva 的扩展指南(包括一个很好的 XAML 行为,可以更好地调整这个用例)。

【讨论】:

  • 谢谢,我会尽量适应我的项目(DockPanel 不适用于 Windows 通用应用程序)
  • @Joe300 我的错。我只是把它放在那里展示布局:)
【解决方案2】:

我已经制作了一个 FullScreenModeTitleBarBehavior(以及一个 FullScreenModeTitle 控件),它可能会满足您的需求。

该行为需要附加到您的主要Page,它允许您指定TitleBar 的前景色和背景色。如果您需要更多颜色,您可以简单地为行为添加更多属性。

它的工作方式是该行为会将Content 从Page 移到FulScreenModeTitle 控件中,该控件基本上与移动的Content 组成一个自定义TitleBar。

// Store the original main page content.
var mainPageContent = _mainPage.Content;
// Clear the content for now.
_mainPage.Content = null;

// Move the content of the main page to our title bar control.
_customTitleBar.SetPageContent(mainPageContent);
// Refill the content with our new title bar control.
_mainPage.Content = _customTitleBar;

您可以在 GitHub 中通过 here 找到完整的源代码。另请注意,此解决方案的灵感来自 Microsoft GitHub 存储库中的这个特定的 sample。


目前我发现的一些问题

您可能已经注意到我们的自定义全屏模式按钮和最小化按钮之间有一个间隙。不幸的是,您无法进一步减少它,因为系统保留了这么多空间(有关更多详细信息,请查看SystemOverlayRightInset)。如果您将自定义按钮移得更近,命中测试将失败,使其无法点击。

我还发现,如果您使用 自定义按钮 退出全屏,这三个系统按钮将无法正常工作,直到您双击 TitleBar 以最大化屏幕。这可能是一个错误。幸运的是,当屏幕处于全屏模式时,最大化按钮将被替换为退出全屏按钮,所以我们可以隐藏我们的自定义按钮 并让系统处理退出。

【讨论】:

  • 不错的解决方案,但您发现的问题非常重要。实际上,当您通过系统按钮退出全屏时,也存在第二个问题。从那以后你有没有设法解决这个问题?
【解决方案3】:

可以区分3种全屏模式 1.进入和退出全屏模式。 2.响应全屏模式的变化。 3. 以全屏模式启动。

您可以参考这个网址 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/FullScreenMode

var view = ApplicationView.GetForCurrentView();
        if (view.IsFullScreenMode)
        {
            view.ExitFullScreenMode();
            rootPage.NotifyUser("Exiting full screen mode", NotifyType.StatusMessage);
            // The SizeChanged event will be raised when the exit from full screen mode is complete.
        }
        else
        {
            if (view.TryEnterFullScreenMode())
            {
                rootPage.NotifyUser("Entering full screen mode", NotifyType.StatusMessage);
                // The SizeChanged event will be raised when the entry to full screen mode is complete.
            }
            else
            {
                rootPage.NotifyUser("Failed to enter full screen mode", NotifyType.ErrorMessage);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    相关资源
    最近更新 更多