Skip to content

Commit

Permalink
User/jecollin/simpleislandapp (#307)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
JesseCol and Jesse Collins authored Sep 21, 2023
1 parent b5917a8 commit 07678a5
Show file tree
Hide file tree
Showing 24 changed files with 1,068 additions and 16 deletions.
60 changes: 60 additions & 0 deletions Samples/Islands/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
page_type: sample
languages:
- cppwinrt
- cpp
products:
- windows
- windows-app-sdk
name: "Simple Island App"
urlFragment: SimpleIslandApp
description: "Shows how to add a WinAppSDK island to a simple Win32 app."
extendedZipContent:
- path: LICENSE
target: LICENSE
---

# Simple Island App

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"
template in Visual Studio, which yields a boilerplate Win32 app that uses Windows APIs that have been around for a long time.

This sample is an "unpackaged" app, so it will run like a win32 app that hasn't been changed for a while.

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.

## What is a WinAppSDK Island?

A WinAppSDK island is a set of APIs that allows an app author to connect two UI frameworks together. This sample demonstrates how
to connect Xaml content into a Win32 app.

## Prerequisites

* See [System requirements for Windows app development](https://docs.microsoft.com/windows/apps/windows-app-sdk/system-requirements).
* 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).

## Building and running the sample

* Open the solution file (`.sln`) in Visual Studio.
* Press Ctrl+Shift+B, or select **Build** \> **Build Solution**.
* Press Ctrl+F5 to launch the app (without attaching a debugger)
> 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.
* Press F5 to launch the app under a debugger.
> Note: When running under a debugger, you may see an "Exception Thrown" dialog box in Visual Studio. You can safely press the "Continue"
button to proceed.
* To run from the command line or File Explorer, navigate to `<arch>/<config>/SimpleIslandApp` directory and run SimpleIslandApp.exe.
* To deploy to another machine, copy the `<arch>/<config>/SimpleIslandApp` directory to that machine and run SimpleIslandApp.exe. The sample
runs on Windows version 17763 and later.

# How to add an Island with Xaml content to your own Win32 app

Here's the basic steps to add an island to your own Win32 app, leveraging code from this sample:
1. Add a NuGet reference to the Microsoft.WindowsAppSDK package (latest stable version).
2. Add a NuGet reference to the Microsoft.Windows.CppWinRT package (latest stable version).
3. Make some modifications to your vcxproj file and main message loop -- look for the tag "Island-support" in the sample code.
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.
The App object is needed for many of the Xaml controls to work, and it also enables metadata lookups for your app.
5. Optional: Add a MainPage to create your first page of Xaml. You can copy it from this sample app.
6. Modify your application to create your Xaml App object. After that, create a DesktopWindowXamlSource object to hold
your Xaml content and position it within your HWND in your app wherever you'd like.

5 changes: 5 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/App.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace SimpleIslandApp
{
}
18 changes: 18 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation.
Licensed under the MIT License. -->
<Application
x:Class="SimpleIslandApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SimpleIslandApp">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
</ResourceDictionary>
</Application.Resources>
</Application>
16 changes: 16 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/App.xaml.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"

#include "App.xaml.h"

using namespace winrt;
using namespace Windows::UI::Xaml;

namespace winrt::SimpleIslandApp::implementation
{
void App::OnLaunched(winrt::Microsoft::UI::Xaml::LaunchActivatedEventArgs const&)
{
}
}

22 changes: 22 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/App.xaml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once

#include "App.xaml.g.h"

#include <winrt/Microsoft.UI.Xaml.Hosting.h>

namespace winrt::SimpleIslandApp::implementation
{
struct App : AppT<App>
{
App()
: m_windowsXamlManager(winrt::Microsoft::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread())
{ }

void OnLaunched(winrt::Microsoft::UI::Xaml::LaunchActivatedEventArgs const&);

private:
winrt::Microsoft::UI::Xaml::Hosting::WindowsXamlManager m_windowsXamlManager{ nullptr };
};
}
26 changes: 26 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/MainPage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"

#include "MainPage.h"

using namespace winrt;
using namespace Microsoft::UI::Xaml;

namespace winrt::SimpleIslandApp::implementation
{
int32_t MainPage::MyProperty()
{
throw hresult_not_implemented();
}

void MainPage::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}

void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
Button().Content(box_value(L"Clicked"));
}
}
29 changes: 29 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/MainPage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once

#include "MainPage.g.h"

namespace winrt::SimpleIslandApp::implementation
{
struct MainPage : MainPageT<MainPage>
{
MainPage()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
}

int32_t MyProperty();
void MyProperty(int32_t value);

void ClickHandler(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
};
}

namespace winrt::SimpleIslandApp::factory_implementation
{
struct MainPage : MainPageT<MainPage, implementation::MainPage>
{
};
}
11 changes: 11 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/MainPage.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace SimpleIslandApp
{
[default_interface]
runtimeclass MainPage : Microsoft.UI.Xaml.Controls.Page
{
MainPage();
Int32 MyProperty;
}
}
24 changes: 24 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Copyright (c) Microsoft Corporation.
Licensed under the MIT License. -->
<Page
x:Class="SimpleIslandApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SimpleIslandApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<Button x:Name="Button" Click="ClickHandler">Click Me</Button>
<TextBox Text="Text goes here" Margin="10" />
</StackPanel>
<WebView2 Grid.Row="1" Source="http://bing.com" />
<Button Grid.Row="2">Last Button</Button>
</Grid>
</Page>
32 changes: 32 additions & 0 deletions Samples/Islands/cpp-win32-unpackaged/Resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by SimpleIslandApp.rc

#define IDS_APP_TITLE 103

#define IDR_MAINFRAME 128
#define IDD_SIMPLEISLANDAPP_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDI_SIMPLEISLANDAPP 107
#define IDI_SMALL 108
#define IDC_SIMPLEISLANDAPP 109
#define IDC_MYICON 2
#ifndef IDC_STATIC
#define IDC_STATIC -1
#endif
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NO_MFC 130
#define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
Loading

0 comments on commit 07678a5

Please sign in to comment.