【发布时间】:2019-01-16 19:24:19
【问题描述】:
我正在使用 C++ 和 XAML 创建 UWP 应用。如何将应用的宽度和高度设置为恒定?
我对 XAML 完全陌生,我想使用 C++/WinRT 创建一个 UWP 应用。我已将页面的宽度设置为 500,将高度设置为 200,但如果我在运行时调整页面大小,然后重新启动应用程序,页面大小仍将是调整后的大小。例如,如果我在运行时将页面的宽度和高度调整为 1000(使用光标拖动页面),当我重新启动应用程序时,页面的宽度和高度仍将是 1000。
这是我的 main.cpp:
#include "pch.h"
using namespace winrt;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Markup;
struct App : ApplicationT<App> {
fire_and_forget OnLaunched(const LaunchActivatedEventArgs&) {
const auto packageFolder = Package::Current().InstalledLocation();
const auto file = co_await packageFolder.GetFileAsync(L"MainPage.xaml");
const auto xaml = co_await FileIO::ReadTextAsync(file);
const auto body = XamlReader::Load(xaml).as<Page>();
auto window = Window::Current();
window.Content(body);
window.Activate();
}
};
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
Application::Start([](auto&&) {make<App>(); });
}
这是我的 MainPage.xaml:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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="500" Height="200">
<Grid HorizontalAlignment="Stretch" Margin="0,0,408,0"
VerticalAlignment="Stretch"/>
</Page>
我希望应用程序在每次启动应用程序时恢复其大小。所以宽度应始终为 500,高度应始终为 200。我不知道为什么会发生这种情况,因为我对此完全陌生,因此我们将不胜感激。
谢谢。
此外,我希望在运行时页面的大小固定,这意味着页面将无法在运行时调整大小。
【问题讨论】: