Skip to content

Commit 07678a5

Browse files
JesseColJesse Collins
andauthored
User/jecollin/simpleislandapp (#307)
* Initial app from Desktop Win32 application template * Add nuget reference to WinAppSDK 1.4 preview * Add nuget reference to CppWinRT * Simple island app now shows an island with a WebView2 and TextBox * Respond to some PR feedback. Removed the globals, improved the error handling. * Respond to PR feedback * PR Feedback: Enable arm64, use manual event loop * PR feedack: move to subdir cpp-win32-unpackaged * Sample instructions: Move to directory named 'Islands' * Update to 1.4 stable, add some comments * Add README * Add SamplesCI-Islands.yml * Update sample md * Add support for tab navigation through the island. * Add some comments, increase warning level. * Small doc update --------- Co-authored-by: Jesse Collins <[email protected]>
1 parent b5917a8 commit 07678a5

24 files changed

+1068
-16
lines changed

Samples/Islands/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- cppwinrt
5+
- cpp
6+
products:
7+
- windows
8+
- windows-app-sdk
9+
name: "Simple Island App"
10+
urlFragment: SimpleIslandApp
11+
description: "Shows how to add a WinAppSDK island to a simple Win32 app."
12+
extendedZipContent:
13+
- path: LICENSE
14+
target: LICENSE
15+
---
16+
17+
# Simple Island App
18+
19+
This sample shows how to add a WinAppSDK island with Xaml content to a Win32 app. It was first created with the C++ "Windows Desktop Application"
20+
template in Visual Studio, which yields a boilerplate Win32 app that uses Windows APIs that have been around for a long time.
21+
22+
This sample is an "unpackaged" app, so it will run like a win32 app that hasn't been changed for a while.
23+
24+
This sample uses Windows App SDK as a "framework package". This means that the Windows App SDK runtime must be installed for it to run.
25+
26+
## What is a WinAppSDK Island?
27+
28+
A WinAppSDK island is a set of APIs that allows an app author to connect two UI frameworks together. This sample demonstrates how
29+
to connect Xaml content into a Win32 app.
30+
31+
## Prerequisites
32+
33+
* See [System requirements for Windows app development](https://docs.microsoft.com/windows/apps/windows-app-sdk/system-requirements).
34+
* Make sure that your development environment is set up correctly&mdash;see [Install tools for developing apps for Windows 10 and Windows 11](https://docs.microsoft.com/windows/apps/windows-app-sdk/set-up-your-development-environment).
35+
36+
## Building and running the sample
37+
38+
* Open the solution file (`.sln`) in Visual Studio.
39+
* Press Ctrl+Shift+B, or select **Build** \> **Build Solution**.
40+
* Press Ctrl+F5 to launch the app (without attaching a debugger)
41+
> Note: If the Windows App SDK runtime isn't installed on the machine, the user will see a message box directing them to a download link.
42+
* Press F5 to launch the app under a debugger.
43+
> Note: When running under a debugger, you may see an "Exception Thrown" dialog box in Visual Studio. You can safely press the "Continue"
44+
button to proceed.
45+
* To run from the command line or File Explorer, navigate to `<arch>/<config>/SimpleIslandApp` directory and run SimpleIslandApp.exe.
46+
* To deploy to another machine, copy the `<arch>/<config>/SimpleIslandApp` directory to that machine and run SimpleIslandApp.exe. The sample
47+
runs on Windows version 17763 and later.
48+
49+
# How to add an Island with Xaml content to your own Win32 app
50+
51+
Here's the basic steps to add an island to your own Win32 app, leveraging code from this sample:
52+
1. Add a NuGet reference to the Microsoft.WindowsAppSDK package (latest stable version).
53+
2. Add a NuGet reference to the Microsoft.Windows.CppWinRT package (latest stable version).
54+
3. Make some modifications to your vcxproj file and main message loop -- look for the tag "Island-support" in the sample code.
55+
4. Add an implementation for your Xaml App object. Feel free to copy the App.* files from this sample, as well as the vcxproj entries.
56+
The App object is needed for many of the Xaml controls to work, and it also enables metadata lookups for your app.
57+
5. Optional: Add a MainPage to create your first page of Xaml. You can copy it from this sample app.
58+
6. Modify your application to create your Xaml App object. After that, create a DesktopWindowXamlSource object to hold
59+
your Xaml content and position it within your HWND in your app wherever you'd like.
60+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
namespace SimpleIslandApp
4+
{
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (c) Microsoft Corporation.
3+
Licensed under the MIT License. -->
4+
<Application
5+
x:Class="SimpleIslandApp.App"
6+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
7+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
8+
xmlns:local="using:SimpleIslandApp">
9+
<Application.Resources>
10+
<ResourceDictionary>
11+
<ResourceDictionary.MergedDictionaries>
12+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
13+
<!-- Other merged dictionaries here -->
14+
</ResourceDictionary.MergedDictionaries>
15+
<!-- Other app resources here -->
16+
</ResourceDictionary>
17+
</Application.Resources>
18+
</Application>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#include "pch.h"
4+
5+
#include "App.xaml.h"
6+
7+
using namespace winrt;
8+
using namespace Windows::UI::Xaml;
9+
10+
namespace winrt::SimpleIslandApp::implementation
11+
{
12+
void App::OnLaunched(winrt::Microsoft::UI::Xaml::LaunchActivatedEventArgs const&)
13+
{
14+
}
15+
}
16+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#pragma once
4+
5+
#include "App.xaml.g.h"
6+
7+
#include <winrt/Microsoft.UI.Xaml.Hosting.h>
8+
9+
namespace winrt::SimpleIslandApp::implementation
10+
{
11+
struct App : AppT<App>
12+
{
13+
App()
14+
: m_windowsXamlManager(winrt::Microsoft::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread())
15+
{ }
16+
17+
void OnLaunched(winrt::Microsoft::UI::Xaml::LaunchActivatedEventArgs const&);
18+
19+
private:
20+
winrt::Microsoft::UI::Xaml::Hosting::WindowsXamlManager m_windowsXamlManager{ nullptr };
21+
};
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#include "pch.h"
4+
5+
#include "MainPage.h"
6+
7+
using namespace winrt;
8+
using namespace Microsoft::UI::Xaml;
9+
10+
namespace winrt::SimpleIslandApp::implementation
11+
{
12+
int32_t MainPage::MyProperty()
13+
{
14+
throw hresult_not_implemented();
15+
}
16+
17+
void MainPage::MyProperty(int32_t /* value */)
18+
{
19+
throw hresult_not_implemented();
20+
}
21+
22+
void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
23+
{
24+
Button().Content(box_value(L"Clicked"));
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#pragma once
4+
5+
#include "MainPage.g.h"
6+
7+
namespace winrt::SimpleIslandApp::implementation
8+
{
9+
struct MainPage : MainPageT<MainPage>
10+
{
11+
MainPage()
12+
{
13+
// Xaml objects should not call InitializeComponent during construction.
14+
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
15+
}
16+
17+
int32_t MyProperty();
18+
void MyProperty(int32_t value);
19+
20+
void ClickHandler(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
21+
};
22+
}
23+
24+
namespace winrt::SimpleIslandApp::factory_implementation
25+
{
26+
struct MainPage : MainPageT<MainPage, implementation::MainPage>
27+
{
28+
};
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
namespace SimpleIslandApp
4+
{
5+
[default_interface]
6+
runtimeclass MainPage : Microsoft.UI.Xaml.Controls.Page
7+
{
8+
MainPage();
9+
Int32 MyProperty;
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!-- Copyright (c) Microsoft Corporation.
2+
Licensed under the MIT License. -->
3+
<Page
4+
x:Class="SimpleIslandApp.MainPage"
5+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
6+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
xmlns:local="using:SimpleIslandApp"
8+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
9+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10+
mc:Ignorable="d">
11+
<Grid Padding="10">
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="50"/>
14+
<RowDefinition Height="*"/>
15+
<RowDefinition Height="50"/>
16+
</Grid.RowDefinitions>
17+
<StackPanel Orientation="Horizontal" Grid.Row="0">
18+
<Button x:Name="Button" Click="ClickHandler">Click Me</Button>
19+
<TextBox Text="Text goes here" Margin="10" />
20+
</StackPanel>
21+
<WebView2 Grid.Row="1" Source="http://bing.com" />
22+
<Button Grid.Row="2">Last Button</Button>
23+
</Grid>
24+
</Page>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
//{{NO_DEPENDENCIES}}
4+
// Microsoft Visual C++ generated include file.
5+
// Used by SimpleIslandApp.rc
6+
7+
#define IDS_APP_TITLE 103
8+
9+
#define IDR_MAINFRAME 128
10+
#define IDD_SIMPLEISLANDAPP_DIALOG 102
11+
#define IDD_ABOUTBOX 103
12+
#define IDM_ABOUT 104
13+
#define IDM_EXIT 105
14+
#define IDI_SIMPLEISLANDAPP 107
15+
#define IDI_SMALL 108
16+
#define IDC_SIMPLEISLANDAPP 109
17+
#define IDC_MYICON 2
18+
#ifndef IDC_STATIC
19+
#define IDC_STATIC -1
20+
#endif
21+
// Next default values for new objects
22+
//
23+
#ifdef APSTUDIO_INVOKED
24+
#ifndef APSTUDIO_READONLY_SYMBOLS
25+
26+
#define _APS_NO_MFC 130
27+
#define _APS_NEXT_RESOURCE_VALUE 129
28+
#define _APS_NEXT_COMMAND_VALUE 32771
29+
#define _APS_NEXT_CONTROL_VALUE 1000
30+
#define _APS_NEXT_SYMED_VALUE 110
31+
#endif
32+
#endif

0 commit comments

Comments
 (0)