简体   繁体   English

Microsoft UWP app-设置窗口大小以匹配背景图像

[英]Microsoft UWP app - Set size of window to match background image

This is probably simple but I cant figure it out (noob to UWP)... 这可能很简单,但我无法弄清楚(对于UWP来说是新手)...

I have an image I want to use as the background for my app (it is really only for desktop - I am not worried about mobile etc). 我有一张图片想用作我的应用程序的背景(实际上仅适用于台式机-我不担心移动设备等)。

I just want to set the size of the window so the app starts at a specific size and the image sits in it without scaling. 我只想设置窗口的大小,以便应用程序以特定的大小启动,并且图像位于其中而不会缩放。

Make sense? 说得通?

Should be easy - I have done it in Windows Forms and its pretty straight forward... 应该很容易-我已经在Windows窗体中完成了它,而且非常简单...

I have tried setting the size of the window (same size as image): 我尝试设置窗口的大小(与图像大小相同):

ApplicationView.PreferredLaunchViewSize = new Size(Height = 800, Width = 525);

But the image never seems to sit in it properly no matter what settings I adjust - either the image properties or grid size etc?? 但是无论我调整什么设置,图像似乎都永远无法正确放置在其中-图像属性或网格大小等?

In XAML design view it all looks OK (to me): 在XAML设计视图中,一切看起来都很好(对我而言):

在此处输入图片说明

But when you run the app its not: 但是,当您运行应用程序时,它不是:

在此处输入图片说明

XAML CODE AS REQUESTED: 要求的XAML代码:

<Page
    x:Class="UWP_Test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWP_Test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Image Source="Assets/titlescreen.bmp" Opacity="0.5" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
    </Grid>
</Page>

XAML.CS code XAML.CS代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace UWP_Test
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            // Set the app window preferred launch view size
            ApplicationView.PreferredLaunchViewSize = new Size(Height = 800, Width = 525);

            // Set app window preferred launch windowing mode
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;


    }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            // move to Start page
            this.Frame.Navigate(typeof(Start));
        }
    }
}

I am sure its simple. 我相信它很简单。

I have added some images 我添加了一些图片

Help me stackoverflow.... You're my only hope... 帮助我stackoverflow ....您是我唯一的希望...

Though it won't work entirely, why not use the Stretch property set to UniformToFill? 尽管不能完全正常工作,但为什么不使用设置为UniformToFill的Stretch属性呢?

<Grid>
    <Image Source="Assets/titlescreen.bmp" Opacity="0.5" Stretch="UniformToFill"></Image>
</Grid>

You could set ImageBrush for Grid's Background like the following code: 您可以将ImageBrush设置为Grid的背景,如以下代码所示:

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="Assets/pander.jpg" Stretch="UniformToFill"></ImageBrush>
    </Grid.Background>
</Grid>

在此处输入图片说明 在此处输入图片说明

Thanks for all the advice but I found out what was going wrong. 感谢所有建议,但我发现出了问题。

The issue was the brackets in this piece of code I was using to set the window size: 问题是我用来设置窗口大小的这段代码中的括号:

ApplicationView.PreferredLaunchViewSize = new Size(Height = 800, Width = 525);

They needed to be curly braces like this: 他们需要像这样的大括号:

ApplicationView.PreferredLaunchViewSize = new Size { Height = 525, Width = 840 };

Once I changed this it is now working fine. 更改此设置后,现在可以正常使用了。

Is it odd that Visual Studio did not highlight these as the wrong sort of braces? Visual Studio没有将这些突出显示为错误的括号是很奇怪的吗? The app compiled and ran fine with no errors?? 该应用程序已编译并且运行正常,没有错误?

Problem solved 问题解决了

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM