簡體   English   中英

Microsoft UWP app-設置窗口大小以匹配背景圖像

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

這可能很簡單,但我無法弄清楚(對於UWP來說是新手)...

我有一張圖片想用作我的應用程序的背景(實際上僅適用於台式機-我不擔心移動設備等)。

我只想設置窗口的大小,以便應用程序以特定的大小啟動,並且圖像位於其中而不會縮放。

說得通?

應該很容易-我已經在Windows窗體中完成了它,而且非常簡單...

我嘗試設置窗口的大小(與圖像大小相同):

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

但是無論我調整什么設置,圖像似乎都永遠無法正確放置在其中-圖像屬性或網格大小等?

在XAML設計視圖中,一切看起來都很好(對我而言):

在此處輸入圖片說明

但是,當您運行應用程序時,它不是:

在此處輸入圖片說明

要求的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代碼

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));
        }
    }
}

我相信它很簡單。

我添加了一些圖片

幫助我stackoverflow ....您是我唯一的希望...

盡管不能完全正常工作,但為什么不使用設置為UniformToFill的Stretch屬性呢?

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

您可以將ImageBrush設置為Grid的背景,如以下代碼所示:

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

在此處輸入圖片說明 在此處輸入圖片說明

感謝所有建議,但我發現出了問題。

問題是我用來設置窗口大小的這段代碼中的括號:

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

他們需要像這樣的大括號:

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

更改此設置后,現在可以正常使用了。

Visual Studio沒有將這些突出顯示為錯誤的括號是很奇怪的嗎? 該應用程序已編譯並且運行正常,沒有錯誤?

問題解決了

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM