- 1 year ago
UWP app with multiple views with different sizes
In a UWP (Universal Windows Platform) app, you can create multiple views with different sizes by utilizing the "Multiple Views" feature. This allows you to have separate windows or views within your app, each with its own size and content. Here's a general overview of how you can achieve this:
Enable Multiple Views: In the
App.xaml.cs
file, locate theOnLaunched
method and add the following code at the beginning to enable multiple views:csharpprotected override async void OnLaunched(LaunchActivatedEventArgs e)
{
if (e.PrelaunchActivated == false)
{
// ...
// Enable Multiple Views
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
// ...
}
}
Create Additional Views: To create additional views, handle the
ViewLifetimeControl.MainViewCreated
event. Inside the event handler, you can create and activate new views with different sizes. Here's an example:csharpViewLifetimeControl viewControl = null;
private async void ViewLifetimeControl_MainViewCreated(object sender, object e)
{
// Create and activate a new view
var newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
viewControl = ViewLifetimeControl.CreateForCurrentView();
viewControl.Title = "New View";
Window.Current.Content = new MainPage();
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
// Assign the size and position of the new view
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId, ViewSizePreference.Default, ApplicationView.GetForCurrentView().Id, ViewSizePreference.Default);
}
Handle View Activation: In the
App.xaml.cs
file, add an event handler for theViewActivated
event. In this event handler, retrieve theViewLifetimeControl
and set theWindow.Current.Content
for the activated view:csharpprotected override async void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.ProtocolForResults && args is ProtocolForResultsActivatedEventArgs protocolArgs)
{
// Handle protocol activation
// ...
}
else if (args is ViewActivatedEventArgs viewArgs)
{
// Handle view activation
if (viewArgs.ViewSwitcher != null)
{
viewControl = ViewLifetimeControl.GetForCurrentView(viewArgs.ViewSwitcher);
viewControl.Title = "Additional View";
Window.Current.Content = new MainPage();
Window.Current.Activate();
}
}
}
Register Event Handlers: Register the event handlers for
MainViewCreated
andViewActivated
in theOnLaunched
method:csharpViewLifetimeControl.MainViewCreated += ViewLifetimeControl_MainViewCreated;
App.Current.Suspending += App_Suspending;
App.Current.Resuming += App_Resuming;
With these steps, you can create multiple views with different sizes in a UWP app. Each view can have its own content and can be displayed in separate windows or as standalone views within your app. Adjust the code according to your app's specific requirements and UI design.