如何删除UWP应用程序中的白色位置(XAML)

ercv8c1e  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(128)

我需要删除这个白色边框(用橙子笔标记)。我希望我的应用程序适合880 px,并且是全宽的。在我的XAML文档中,我做了以下操作

<Page
    x:Class="FirstScreen.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FirstScreen"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Width="880" Height="600">

    <Grid Width="880" Height="600" Background="Black">

    </Grid>
</Page>

但我仍然有白色边框!我需要什么,我的应用程序将编译出这个边框?

xv8emn3q

xv8emn3q1#

您可以使用TryResizeView(Size)函数停止调整窗口大小,但当用户使用UI时,不建议通过代码隐藏来调整大小。
我将在这里分享解决方案,但再次不推荐。
在App.xaml.cs的OnLaunched事件中,完成所有与UI相关的代码后,在结尾处放置以下代码行

ApplicationView.GetForCurrentView().TryResizeView(size);
        ApplicationView.GetForCurrentView().VisibleBoundsChanged += App_VisibleBoundsChanged;

并为“应用程序可见边界已更改”创建事件

private void App_VisibleBoundsChanged(ApplicationView sender, object args)
    { 
        ApplicationView.GetForCurrentView().TryResizeView(size);
    }

声明Size的全局变量

Size size = new Size(800, 600);

这将使您的应用程序,始终工作在指定的大小。但用户觉得像一个小故障的应用程序,当他们试图调整大小。
下面是完整的App.xaml.cs代码

sealed partial class App : Application
{
    Size size = new Size(800, 600);

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
    }


    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        ApplicationView.GetForCurrentView().TryResizeView(size);
        ApplicationView.GetForCurrentView().VisibleBoundsChanged += App_VisibleBoundsChanged;
    }

    private void App_VisibleBoundsChanged(ApplicationView sender, object args)
    {
        ApplicationView.GetForCurrentView().TryResizeView(size);
    }
}
5m1hhzi4

5m1hhzi42#

您将Grid的宽度设为880,因此网格不会填充所有窗口。只需删除您添加的宽度和高度。

相关问题