0
votes

I am creating a UWP app using C++ and XAML. How can I set the app's width and height to be constant?

I am completely new to XAML and I want to create a UWP app using C++/WinRT. I have set the page's width to 500 and the height to 200 but if I resize the page during runtime, and then I restart the app, the size of the page will still be the resized size. For example if I resize the page's width and height too 1000 during runtime (using my cursor to drag the page), when I then restart the app, the page's width and height will still be 1000.

Here is my 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>(); });
}

And here is my 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>

I would want the app to restore its size each time a start the app. So the width should always be 500 and the height should always be 200 on startup. I have no idea why this happens since I am completely new to this so any help would be appreciated.

Thanks.

Additionally I would want a fixed size of the page during runtime, meaning that the page will not be able to resize during runtime.

1

1 Answers

0
votes

UWP apps do not only run on desktop, it also run on tablet, phone etc. So, what you want to set a fixed size page was not realistic. But you could call the ApplicationView.TryResizeView(Size) Method to change the size of the view.

If you still want a fixed size of the page during runtime, you could handle the page's SizeChanged event, in its event handler, you could call ApplicationView.TryResizeView method to resize it to the original size.