Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Update to supported version of .net sdk 3.1.412, update C++ WinRT package, quite warnings, minor cleanup in XamlApplication #335

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props')" />
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTEnabled>true</CppWinRTEnabled>
<MinimalCoreWin>true</MinimalCoreWin>
Expand Down Expand Up @@ -99,9 +99,9 @@
<WarningLevel>Level4</WarningLevel>
<AdditionalOptions>%(AdditionalOptions) /bigobj</AdditionalOptions>
<DisableSpecificWarnings>28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_WINRT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;%(PreprocessorDefinitions);WINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP</PreprocessorDefinitions>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<GenerateXMLDocumentationFiles>true</GenerateXMLDocumentationFiles>
<GenerateXMLDocumentationFiles>false</GenerateXMLDocumentationFiles>
<ControlFlowGuard>Guard</ControlFlowGuard>
</ClCompile>
<Link>
Expand Down Expand Up @@ -151,16 +151,23 @@
<None Include="packages.config" />
<None Include="ReadMe.md" />
</ItemGroup>
<ItemGroup>
<SDKReference Include="WindowsDesktop, Version=10.0.18362.0">
<Name>Windows Desktop Extensions for the UWP</Name>
</SDKReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.210803.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.210803.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
</ImportGroup>
<Target Name="Pack" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.210825.3\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.210803.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.210803.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
</Target>
</Project>
13 changes: 12 additions & 1 deletion Microsoft.Toolkit.Win32.UI.XamlApplication/ReadMe.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
Xaml Application for Win32
# Xaml Application Object for Win32 Apps
Xaml Application object implementation, used in Xaml Island consuming projects (WinForms, WPF, C++)

## To Build

Install the .Net 3 SDK from [this location](https://dotnet.microsoft.com/download/visual-studio-sdks).

## To Run

- In the XamlHost\Samples node of Solution Explorer
- Set Microsoft.Toolkit.Sample.Forms.App or Microsoft.Toolkit.Sample.Wpf.App as the startup project.
- F5
54 changes: 18 additions & 36 deletions Microsoft.Toolkit.Win32.UI.XamlApplication/XamlApplication.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
#include "pch.h"
#include "pch.h"

#include "XamlApplication.h"

namespace xaml = ::winrt::Windows::UI::Xaml;

extern "C" {
WINBASEAPI HMODULE WINAPI LoadLibraryExW(_In_ LPCWSTR lpLibFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags);
WINBASEAPI HMODULE WINAPI GetModuleHandleW(_In_opt_ LPCWSTR lpModuleName);
WINUSERAPI BOOL WINAPI PeekMessageW(_Out_ LPMSG lpMsg, _In_opt_ HWND hWnd, _In_ UINT wMsgFilterMin, _In_ UINT wMsgFilterMax, _In_ UINT wRemoveMsg);
WINUSERAPI LRESULT WINAPI DispatchMessageW(_In_ CONST MSG* lpMsg);
namespace winrt
{
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Hosting;
}

namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
{
XamlApplication::XamlApplication(winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> providers)
XamlApplication::XamlApplication(const winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider>& providers)
{
for(auto provider : providers)
for (auto&& provider : providers)
{
m_providers.Append(provider);
}

Initialize();
}

XamlApplication::XamlApplication()
{
}

void XamlApplication::Initialize()
{
const auto out = outer();
if (out)
{
winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider provider(nullptr);
winrt::check_hresult(out->QueryInterface(
winrt::guid_of<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider>(),
reinterpret_cast<void**>(winrt::put_abi(provider))));
m_providers.Append(provider);
}
winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider provider{ nullptr };
winrt::check_hresult(outer()->QueryInterface(winrt::guid_of<decltype(provider)>(), winrt::put_abi(provider)));
m_providers.Append(provider);

const auto dispatcherQueue = winrt::Windows::System::DispatcherQueue::GetForCurrentThread();
if (!dispatcherQueue)
{
m_executionMode = ExecutionMode::Win32;
m_windowsXamlManager = xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
}
else
{
m_executionMode = ExecutionMode::UWP;
m_windowsXamlManager = winrt::Hosting::WindowsXamlManager::InitializeForCurrentThread();
}
}

Expand Down Expand Up @@ -84,7 +66,7 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
Close();
}

xaml::Markup::IXamlType XamlApplication::GetXamlType(xaml::Interop::TypeName const& type)
winrt::Markup::IXamlType XamlApplication::GetXamlType(winrt::Interop::TypeName const& type)
{
for (const auto& provider : m_providers)
{
Expand All @@ -98,7 +80,7 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
return nullptr;
}

xaml::Markup::IXamlType XamlApplication::GetXamlType(winrt::hstring const& fullName)
winrt::Markup::IXamlType XamlApplication::GetXamlType(winrt::hstring const& fullName)
{
for (const auto& provider : m_providers)
{
Expand All @@ -112,9 +94,9 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
return nullptr;
}

winrt::com_array<xaml::Markup::XmlnsDefinition> XamlApplication::GetXmlnsDefinitions()
winrt::com_array<winrt::Markup::XmlnsDefinition> XamlApplication::GetXmlnsDefinitions()
{
std::list<xaml::Markup::XmlnsDefinition> definitions;
std::list<winrt::Markup::XmlnsDefinition> definitions;
for (const auto& provider : m_providers)
{
auto defs = provider.GetXmlnsDefinitions();
Expand All @@ -124,10 +106,10 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
}
}

return winrt::com_array<xaml::Markup::XmlnsDefinition>(definitions.begin(), definitions.end());
return winrt::com_array<winrt::Markup::XmlnsDefinition>(definitions.begin(), definitions.end());
}

winrt::Windows::Foundation::Collections::IVector<xaml::Markup::IXamlMetadataProvider> XamlApplication::MetadataProviders()
winrt::Windows::Foundation::Collections::IVector<winrt::Markup::IXamlMetadataProvider> XamlApplication::MetadataProviders()
{
return m_providers;
}
Expand Down Expand Up @@ -157,4 +139,4 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::factory_implementation
::FreeLibrary(module);
}
}
}
}
12 changes: 4 additions & 8 deletions Microsoft.Toolkit.Win32.UI.XamlApplication/XamlApplication.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#pragma once
#pragma once

#include "pch.h"
#include "XamlApplication.g.h"
#include <winrt/Windows.UI.Xaml.Hosting.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <Windows.h>

namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
{
Expand All @@ -17,8 +14,8 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
class XamlApplication : public XamlApplicationT<XamlApplication, Windows::UI::Xaml::Markup::IXamlMetadataProvider>
{
public:
XamlApplication();
XamlApplication(winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> providers);
XamlApplication() = default;
XamlApplication(const winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider>& providers);
~XamlApplication();

void Initialize();
Expand All @@ -38,7 +35,6 @@ namespace winrt::Microsoft::Toolkit::Win32::UI::XamlHost::implementation
}

private:
ExecutionMode m_executionMode = ExecutionMode::Win32;
winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager m_windowsXamlManager = nullptr;
winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> m_providers = winrt::single_threaded_vector<Windows::UI::Xaml::Markup::IXamlMetadataProvider>();
bool m_bIsClosed = false;
Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Win32.UI.XamlApplication/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.CppWinRT" version="2.0.190506.1" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.210825.3" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.210803.1" targetFramework="native" />
</packages>
16 changes: 9 additions & 7 deletions Microsoft.Toolkit.Win32.UI.XamlApplication/pch.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once
#pragma once

#include <SDKDDKVer.h>
//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <WinUser.h>

#include <unknwn.h> // To enable support for non-WinRT interfaces, unknwn.h must be included before any C++/WinRT headers.
#include <wil/cppwinrt.h>

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#undef GetCurrentTime
#include <winrt/Windows.UI.Xaml.Hosting.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include <winrt/Windows.System.h>

#include <wil/com.h>

#include <wil/resource.h>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.409"
"version": "3.1.412"
}
}