win10 UWP 全屏

时间:2021-10-06 09:10:17

win10 可以全屏软件或窗口,窗口有一般、最小化、最大化。我们有新的API设置我们软件是全屏,是窗口。我们可以使用ApplicationView让我们软件全屏,取消。

下面是一个简单的例子,判断我们软件是不是全屏,如果是,就不全屏,代码在一个ToggleButton

ApplicationView view = ApplicationView.GetForCurrentView();

bool isInFullScreenMode = view.IsFullScreenMode;

if (isInFullScreenMode)
{
    view.ExitFullScreenMode();
}
else
{
    view.TryEnterFullScreenMode();
}

IsFullScreenMode为true,现在应用全屏

ExitFullScreenMode退出全屏

TryEnterFullScreenMode进入全屏,进入全屏成功true

如果窗口改变需要知道,可以注册Window.Current.SizeChanged

public class IsFullScreenModeTrigger : StateTriggerBase
{
    public IsFullScreenModeTrigger()
    {
        ApplicationView view = ApplicationView.GetForCurrentView();

        SetActive(view.IsFullScreenMode);

        Window.Current.SizeChanged += CurrentWindow_SizeChanged;
    }

    private void CurrentWindow_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
    {
        ApplicationView view = ApplicationView.GetForCurrentView();

        SetActive(view.IsFullScreenMode);
    }
}
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="InFullScreen">
                <VisualState.StateTriggers>
                    <local:IsFullScreenModeTrigger />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="IsFullScreenText.Text" Value="In full screen mode" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <StackPanel HorizontalAlignment="Center">
        <Button Click="Button_Click"
                Content="Toggle full screen"></Button>
        <TextBlock x:Name="IsFullScreenText"
                    FontSize="72"
                    TextWrapping="Wrap"
                    Text="Not in full screen mode"/>
    </StackPanel>
</Grid>  

在我们应用变为全屏,textblock就会In full screen mode

我们可以设置PreferredLaunchWindowingMode,在我们应用打开

ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;

ApplicationViewWindowingMode可以Auto ,PreferredLaunchViewSize设置窗口和ApplicationView.PreferredLaunchViewSize,如果没有设置ApplicationView.PreferredLaunchViewSize会使用上次关闭窗口, FullScreen

win10有很简单的API可以应用全屏,在电脑,我们经常用窗口,手机经常使用全屏。

http://igrali.com/2015/06/21/full-screen-mode-in-windows-10-universal-apps/