From 3d18366bbc63e784e6d56156a49618b5ad77a1f4 Mon Sep 17 00:00:00 2001 From: Soumadeep Mazumdar Date: Wed, 20 Nov 2024 03:36:09 +0530 Subject: [PATCH] Sample update for 1.0.2950-prerelease (#259) * Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 132.0.2950.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.2950-prerelease --------- Co-authored-by: WebView2 Github Bot --- SampleApps/WebView2APISample/AppWindow.cpp | 29 +- SampleApps/WebView2APISample/AppWindow.h | 17 +- .../ScenarioAddHostObject.cpp | 187 ++++----- .../WebView2APISample/ScenarioAddHostObject.h | 1 + SampleApps/WebView2APISample/Toolbar.cpp | 9 + SampleApps/WebView2APISample/Toolbar.h | 1 + .../WebView2APISample.vcxproj | 4 +- .../assets/ScenarioAddHostObject.html | 371 ++++++++++-------- SampleApps/WebView2APISample/packages.config | 2 +- .../WebView2WindowsFormsBrowser.csproj | 2 +- .../WebView2WpfBrowser/MainWindow.xaml.cs | 35 +- .../WebView2WpfBrowser.csproj | 2 +- .../WebView2_WinUI3_Sample/App.xaml.cs | 16 +- SampleApps/WebView2_WinUI3_Sample/readme.md | 51 ++- 14 files changed, 434 insertions(+), 293 deletions(-) diff --git a/SampleApps/WebView2APISample/AppWindow.cpp b/SampleApps/WebView2APISample/AppWindow.cpp index 463ecdd6..3c9dbcd4 100644 --- a/SampleApps/WebView2APISample/AppWindow.cpp +++ b/SampleApps/WebView2APISample/AppWindow.cpp @@ -39,6 +39,7 @@ #include "ScenarioDragDrop.h" #include "ScenarioExtensionsManagement.h" #include "ScenarioFileSystemHandleShare.h" +#include "ScenarioFileTypePolicy.h" #include "ScenarioIFrameDevicePermission.h" #include "ScenarioNavigateWithWebResourceRequest.h" #include "ScenarioNonClientRegionSupport.h" @@ -49,7 +50,6 @@ #include "ScenarioSharedBuffer.h" #include "ScenarioSharedWorkerWRR.h" #include "ScenarioThrottlingControl.h" -#include "ScenarioFileTypePolicy.h" #include "ScenarioVirtualHostMappingForPopUpWindow.h" #include "ScenarioVirtualHostMappingForSW.h" #include "ScenarioWebMessage.h" @@ -201,7 +201,7 @@ AppWindow::AppWindow( WCHAR szTitle[s_maxLoadString]; // The title bar text LoadStringW(g_hInstance, IDS_APP_TITLE, szTitle, s_maxLoadString); m_appTitle = szTitle; - + DWORD windowStyle = WS_OVERLAPPEDWINDOW; if (userDataFolderParam.length() > 0) { m_userDataFolder = userDataFolderParam; @@ -210,15 +210,15 @@ AppWindow::AppWindow( if (customWindowRect) { m_mainWindow = CreateWindowExW( - WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, WS_OVERLAPPEDWINDOW, - windowRect.left, windowRect.top, windowRect.right - windowRect.left, + WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, windowStyle, windowRect.left, + windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, nullptr, nullptr, g_hInstance, nullptr); } else { m_mainWindow = CreateWindowExW( - WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, - 0, CW_USEDEFAULT, 0, nullptr, nullptr, g_hInstance, nullptr); + WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, windowStyle, CW_USEDEFAULT, 0, + CW_USEDEFAULT, 0, nullptr, nullptr, g_hInstance, nullptr); } m_appBackgroundImageHandle = (HBITMAP)LoadImage( @@ -1429,11 +1429,11 @@ HRESULT AppWindow::OnCreateEnvironmentCompleted( m_webViewEnvironment = environment; if (m_webviewOption.entry == WebViewCreateEntry::EVER_FROM_CREATE_WITH_OPTION_MENU || - m_creationModeId == IDM_CREATION_MODE_HOST_INPUT_PROCESSING) + m_creationModeId == IDM_CREATION_MODE_HOST_INPUT_PROCESSING + ) { return CreateControllerWithOptions(); } - auto webViewEnvironment3 = m_webViewEnvironment.try_query(); if (webViewEnvironment3 && (m_dcompDevice || m_wincompCompositor)) @@ -1625,7 +1625,6 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted( m_creationModeId == IDM_CREATION_MODE_TARGET_DCOMP); NewComponent(this); NewComponent(this, &m_toolbar); - m_webView3 = coreWebView2.try_query(); if (m_webView3) { @@ -1969,13 +1968,13 @@ void AppWindow::RegisterEventHandlers() RunAsync( [this]() { - std::wstring message = - L"We detected there is a critical update for WebView2 runtime."; + std::wstring message = L"We detected there is a critical " + L"update for WebView2 runtime."; if (m_webView) { message += L"Do you want to restart the app? \n\n"; - message += - L"Click No if you only want to re-create the webviews. \n"; + message += L"Click No if you only want to re-create the " + L"webviews. \n"; message += L"Click Cancel for no action. \n"; } int response = MessageBox( @@ -2036,11 +2035,11 @@ void AppWindow::ResizeEverything() RECT availableBounds = {0}; GetClientRect(m_mainWindow, &availableBounds); - if (!m_containsFullscreenElement) + if (!m_containsFullscreenElement + ) { availableBounds = m_toolbar.Resize(availableBounds); } - if (auto view = GetComponent()) { view->SetBounds(availableBounds); diff --git a/SampleApps/WebView2APISample/AppWindow.h b/SampleApps/WebView2APISample/AppWindow.h index d1fe92a4..c00c694a 100644 --- a/SampleApps/WebView2APISample/AppWindow.h +++ b/SampleApps/WebView2APISample/AppWindow.h @@ -43,12 +43,12 @@ struct WebViewCreateOption } WebViewCreateOption( const std::wstring& profile_, bool inPrivate, const std::wstring& downloadPath, - const std::wstring& scriptLocale_, WebViewCreateEntry entry_, bool useOSRegion_) + const std::wstring& scriptLocale_, WebViewCreateEntry entry_, bool useOSRegion_ + ) : profile(profile_), isInPrivate(inPrivate), downloadPath(downloadPath), scriptLocale(scriptLocale_), entry(entry_), useOSRegion(useOSRegion_) { } - WebViewCreateOption(const WebViewCreateOption& opt) { profile = opt.profile; @@ -87,15 +87,10 @@ class AppWindow { public: AppWindow( - UINT creationModeId, - const WebViewCreateOption& opt, - const std::wstring& initialUri = L"", - const std::wstring& userDataFolderParam = L"", - bool isMainWindow = false, - std::function webviewCreatedCallback = nullptr, - bool customWindowRect = false, - RECT windowRect = {0}, - bool shouldHaveToolbar = true, + UINT creationModeId, const WebViewCreateOption& opt, + const std::wstring& initialUri = L"", const std::wstring& userDataFolderParam = L"", + bool isMainWindow = false, std::function webviewCreatedCallback = nullptr, + bool customWindowRect = false, RECT windowRect = {0}, bool shouldHaveToolbar = true, bool isPopup = false); ~AppWindow(); diff --git a/SampleApps/WebView2APISample/ScenarioAddHostObject.cpp b/SampleApps/WebView2APISample/ScenarioAddHostObject.cpp index 4e133ec4..c219f63c 100644 --- a/SampleApps/WebView2APISample/ScenarioAddHostObject.cpp +++ b/SampleApps/WebView2APISample/ScenarioAddHostObject.cpp @@ -32,48 +32,50 @@ ScenarioAddHostObject::ScenarioAddHostObject(AppWindow* appWindow) m_hostObject = Microsoft::WRL::Make( [appWindow = m_appWindow](std::function callback) - { - appWindow->RunAsync(callback); - }); + { appWindow->RunAsync(callback); }); CHECK_FAILURE(m_webView->add_NavigationStarting( Microsoft::WRL::Callback( - [this, sampleUri](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT - { - wil::unique_cotaskmem_string navigationTargetUri; - CHECK_FAILURE(args->get_Uri(&navigationTargetUri)); - std::wstring uriTarget(navigationTargetUri.get()); - - if (AreFileUrisEqual(sampleUri, uriTarget)) - { - //! [AddHostObjectToScript] - VARIANT remoteObjectAsVariant = {}; - m_hostObject.query_to(&remoteObjectAsVariant.pdispVal); - remoteObjectAsVariant.vt = VT_DISPATCH; - - // We can call AddHostObjectToScript multiple times in a row without - // calling RemoveHostObject first. This will replace the previous object - // with the new object. In our case this is the same object and everything - // is fine. - CHECK_FAILURE( - m_webView->AddHostObjectToScript(L"sample", &remoteObjectAsVariant)); - remoteObjectAsVariant.pdispVal->Release(); - //! [AddHostObjectToScript] - } - else - { - // We can call RemoveHostObject multiple times in a row without - // calling AddHostObjectToScript first. This will produce an error - // result so we ignore the failure. - m_webView->RemoveHostObjectFromScript(L"sample"); - - // When we navigate elsewhere we're off of the sample - // scenario page and so should remove the scenario. - m_appWindow->DeleteComponent(this); - } - - return S_OK; - }).Get(), &m_navigationStartingToken)); + [this, sampleUri]( + ICoreWebView2* sender, + ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT + { + wil::unique_cotaskmem_string navigationTargetUri; + CHECK_FAILURE(args->get_Uri(&navigationTargetUri)); + std::wstring uriTarget(navigationTargetUri.get()); + + if (AreFileUrisEqual(sampleUri, uriTarget)) + { + //! [AddHostObjectToScript] + VARIANT remoteObjectAsVariant = {}; + m_hostObject.query_to(&remoteObjectAsVariant.pdispVal); + remoteObjectAsVariant.vt = VT_DISPATCH; + + // We can call AddHostObjectToScript multiple times in a row without + // calling RemoveHostObject first. This will replace the previous object + // with the new object. In our case this is the same object and everything + // is fine. + CHECK_FAILURE( + m_webView->AddHostObjectToScript(L"sample", &remoteObjectAsVariant)); + remoteObjectAsVariant.pdispVal->Release(); + //! [AddHostObjectToScript] + } + else + { + // We can call RemoveHostObject multiple times in a row without + // calling AddHostObjectToScript first. This will produce an error + // result so we ignore the failure. + m_webView->RemoveHostObjectFromScript(L"sample"); + + // When we navigate elsewhere we're off of the sample + // scenario page and so should remove the scenario. + m_appWindow->DeleteComponent(this); + } + + return S_OK; + }) + .Get(), + &m_navigationStartingToken)); wil::com_ptr webview2_4 = m_webView.try_query(); if (webview2_4) @@ -83,63 +85,70 @@ ScenarioAddHostObject::ScenarioAddHostObject(AppWindow* appWindow) CHECK_FAILURE(webview2_4->add_FrameCreated( Callback( [this]( - ICoreWebView2* sender, - ICoreWebView2FrameCreatedEventArgs* args) -> HRESULT - { - wil::com_ptr webviewFrame; - CHECK_FAILURE(args->get_Frame(&webviewFrame)); - - wil::unique_cotaskmem_string name; - CHECK_FAILURE(webviewFrame->get_Name(&name)); - if (std::wcscmp(name.get(), L"iframe_name") == 0) - { - //! [AddHostObjectToScriptWithOrigins] - wil::unique_variant remoteObjectAsVariant; - // It will throw if m_hostObject fails the QI, but because it is our object - // it should always succeed. - m_hostObject.query_to(&remoteObjectAsVariant.pdispVal); - remoteObjectAsVariant.vt = VT_DISPATCH; - - // Create list of origins which will be checked. - // iframe will have access to host object only if its origin belongs - // to this list. - LPCWSTR origin = L"https://appassets.example/"; - - CHECK_FAILURE(webviewFrame->AddHostObjectToScriptWithOrigins( - L"sample", &remoteObjectAsVariant, 1, &origin)); - //! [AddHostObjectToScriptWithOrigins] - } - - // Subscribe to frame name changed event - webviewFrame->add_NameChanged( - Callback( - [this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT { - wil::unique_cotaskmem_string newName; - CHECK_FAILURE(sender->get_Name(&newName)); - // Handle name changed event - return S_OK; - }).Get(), NULL); - - // Subscribe to frame destroyed event - webviewFrame->add_Destroyed( - Callback( - [this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT { - /*Cleanup on frame destruction*/ - return S_OK; - }) - .Get(), - NULL); - return S_OK; - }).Get(), &m_frameCreatedToken)); + ICoreWebView2* sender, ICoreWebView2FrameCreatedEventArgs* args) -> HRESULT + { + wil::com_ptr webviewFrame; + CHECK_FAILURE(args->get_Frame(&webviewFrame)); + + wil::unique_cotaskmem_string name; + CHECK_FAILURE(webviewFrame->get_Name(&name)); + if (std::wcscmp(name.get(), L"iframe_name") == 0) + { + //! [AddHostObjectToScriptWithOrigins] + wil::unique_variant remoteObjectAsVariant; + // It will throw if m_hostObject fails the QI, but because it is our + // object it should always succeed. + m_hostObject.query_to(&remoteObjectAsVariant.pdispVal); + remoteObjectAsVariant.vt = VT_DISPATCH; + + // Create list of origins which will be checked. + // iframe will have access to host object only if its origin belongs + // to this list. + LPCWSTR origin = L"https://appassets.example/"; + + CHECK_FAILURE(webviewFrame->AddHostObjectToScriptWithOrigins( + L"sample", &remoteObjectAsVariant, 1, &origin)); + //! [AddHostObjectToScriptWithOrigins] + } + + // Subscribe to frame name changed event + webviewFrame->add_NameChanged( + Callback( + [this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT + { + wil::unique_cotaskmem_string newName; + CHECK_FAILURE(sender->get_Name(&newName)); + // Handle name changed event + return S_OK; + }) + .Get(), + NULL); + + // Subscribe to frame destroyed event + webviewFrame->add_Destroyed( + Callback( + [this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT + { + /*Cleanup on frame destruction*/ + return S_OK; + }) + .Get(), + NULL); + return S_OK; + }) + .Get(), + &m_frameCreatedToken)); } - CHECK_FAILURE(m_webView->Navigate(sampleUri.c_str())); } - ScenarioAddHostObject::~ScenarioAddHostObject() { m_webView->RemoveHostObjectFromScript(L"sample"); m_webView->remove_NavigationStarting(m_navigationStartingToken); + if (m_navigationCompletedToken.value) + { + m_webView->remove_NavigationCompleted(m_navigationCompletedToken); + } wil::com_ptr webview2_4 = m_webView.try_query(); if (webview2_4) { diff --git a/SampleApps/WebView2APISample/ScenarioAddHostObject.h b/SampleApps/WebView2APISample/ScenarioAddHostObject.h index 822ef2d0..bda254ce 100644 --- a/SampleApps/WebView2APISample/ScenarioAddHostObject.h +++ b/SampleApps/WebView2APISample/ScenarioAddHostObject.h @@ -24,5 +24,6 @@ class ScenarioAddHostObject : public ComponentBase wil::com_ptr m_hostObject; EventRegistrationToken m_navigationStartingToken = {}; + EventRegistrationToken m_navigationCompletedToken = {}; EventRegistrationToken m_frameCreatedToken = {}; }; diff --git a/SampleApps/WebView2APISample/Toolbar.cpp b/SampleApps/WebView2APISample/Toolbar.cpp index 35718d92..deee617d 100644 --- a/SampleApps/WebView2APISample/Toolbar.cpp +++ b/SampleApps/WebView2APISample/Toolbar.cpp @@ -56,6 +56,15 @@ void Toolbar::SetItemEnabled(Item item, bool enabled) EnableWindow(m_items[item], enabled); } +void Toolbar::Hide() +{ + DisableAllItems(); + for (HWND hwnd : m_items) + { + ShowWindow(hwnd, SW_HIDE); + } +} + void Toolbar::DisableAllItems() { for (HWND hwnd : m_items) diff --git a/SampleApps/WebView2APISample/Toolbar.h b/SampleApps/WebView2APISample/Toolbar.h index 88f89717..d6c0b05d 100644 --- a/SampleApps/WebView2APISample/Toolbar.h +++ b/SampleApps/WebView2APISample/Toolbar.h @@ -35,6 +35,7 @@ class Toolbar void UpdateDpiAndTextScale(); void SelectAll(); void SelectAddressBar(); + void Hide(); private: int GetItemLogicalWidth(Item item, int clientLogicalWidth) const; diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index a972b9a4..b4f233d4 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -486,13 +486,13 @@ - + 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}. - + diff --git a/SampleApps/WebView2APISample/assets/ScenarioAddHostObject.html b/SampleApps/WebView2APISample/assets/ScenarioAddHostObject.html index 52602cf8..be89318d 100644 --- a/SampleApps/WebView2APISample/assets/ScenarioAddHostObject.html +++ b/SampleApps/WebView2APISample/assets/ScenarioAddHostObject.html @@ -1,164 +1,221 @@ + - AddHostObjectToScript Sample + AddHostObjectToScript Sample + -

AddHostObjectToScript Sample

-

The following buttons interact with the chrome.webview.hostObjects.sample object. Open DevTools console to try running whatever code you like on this object.

-

Get Property

- - -
- - - -
- -

Set Property

- - -
- - - -
- - - -
- -

Indexed Property

- - -
- - - -
- -

Invoke Method

- - -
- - - -
- -

Invoke Callback

- - -
- -

Date Objects

- - -
- - -
- - - - +

AddHostObjectToScript Sample

+

The following buttons interact with the chrome.webview.hostObjects.sample object. Open DevTools + console to try running whatever code you like on this object.

+

Get Property

+ + +
+ + + +
+ +

Set Property

+ + +
+ + + +
+ + + +
+ +

Indexed Property

+ + +
+ + + +
+ +

Invoke Method

+ + +
+ + + +
+ +

Invoke Callback

+ + +
+ +

Date Objects

+ + +
+ + +
+ + + +
+

IFrame

+
+ + + \ No newline at end of file diff --git a/SampleApps/WebView2APISample/packages.config b/SampleApps/WebView2APISample/packages.config index de9ae640..95f96ccf 100644 --- a/SampleApps/WebView2APISample/packages.config +++ b/SampleApps/WebView2APISample/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index 9f02b96f..e7dbd0c0 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -25,7 +25,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index 6c106371..e6a86194 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -3806,24 +3806,53 @@ void WebView_FileTypePolicy_DOMContentLoaded(object sender, CoreWebView2DOMConte } void DedicatedWorkerCreatedExecuted(object target, ExecutedRoutedEventArgs e) + { + RegisterForDedicatedWorkerCreated(); + } + + void RegisterForDedicatedWorkerCreated() { } void ServiceWorkerRegisteredExecuted(object target, ExecutedRoutedEventArgs e) + { + RegisterForServiceWorkerRegistered(); + } + void RegisterForServiceWorkerRegistered() { } - private async void GetServiceWorkerRegistrationsExecuted(object target, ExecutedRoutedEventArgs e) + private void GetServiceWorkerRegistrationsExecuted(object target, ExecutedRoutedEventArgs e) + { + GetServiceWorkerRegistrations(); + } + + async void GetServiceWorkerRegistrations() { await Task.Delay(0); } - async void GetServiceWorkerRegisteredForScopeExecuted(object target, ExecutedRoutedEventArgs e) + void GetServiceWorkerRegisteredForScopeExecuted(object target, ExecutedRoutedEventArgs e) + { + GetServiceWorkerRegisteredForScope(); + } + + async void GetServiceWorkerRegisteredForScope() { await Task.Delay(0); } void SharedWorkerManagerExecuted(object target, ExecutedRoutedEventArgs e) { + RegisterForSharedWorkerCreated(); + } + + void RegisterForSharedWorkerCreated() + { + } + private void GetSharedWorkersExecuted(object target, ExecutedRoutedEventArgs e) + { + GetSharedWorkers(); } - private async void GetSharedWorkersExecuted(object target, ExecutedRoutedEventArgs e) + + async void GetSharedWorkers() { await Task.Delay(0); } diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj index b0575eaf..e23a6214 100644 --- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj +++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj @@ -61,7 +61,7 @@ - + diff --git a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/App.xaml.cs b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/App.xaml.cs index 2a407874..e9b4de24 100644 --- a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/App.xaml.cs +++ b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/App.xaml.cs @@ -22,15 +22,15 @@ public App() { this.InitializeComponent(); - // If you're shipping a fixed-version WebView2 Runtime with your app, un-comment the - // following lines of code, and change the version number to the version number of the - // WebView2 Runtime that you're packaging and shipping to users: + // If your shipping a fixed version WebView2 SDK with your application you will need + // to use the following code (update the runtime version to what your shipping. + + //StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; + //String fixedPath = Path.Combine(localFolder.Path, "FixedRuntime\\95.0.1020.53"); + //Debug.WriteLine($"Launch path [{localFolder.Path}]"); + //Debug.WriteLine($"FixedRuntime path [{fixedPath}]"); + //Environment.SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", fixedPath); - // StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; - // String fixedPath = Path.Combine(localFolder.Path, "FixedRuntime\\130.0.2849.39"); - // Debug.WriteLine($"Launch path [{localFolder.Path}]"); - // Debug.WriteLine($"FixedRuntime path [{fixedPath}]"); - // Environment.SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", fixedPath); } /// diff --git a/SampleApps/WebView2_WinUI3_Sample/readme.md b/SampleApps/WebView2_WinUI3_Sample/readme.md index 43c37dde..0566c9e7 100644 --- a/SampleApps/WebView2_WinUI3_Sample/readme.md +++ b/SampleApps/WebView2_WinUI3_Sample/readme.md @@ -1,9 +1,50 @@ -# WinUI 3 (Windows App SDK) sample app +# Introduction +This sample shows off using a WebView2 control in a WinUi 3 Windows SDK Packaged application. + +It also optionaly shows how you would update the application to ship with a fixed WebView2 version instead of using the version installed and running on the Windows computer. + +# Relevant directories + +| Directory | Contents | +--- | --- | +| WebView2_WinUI3_Sample | Project code | +| WebView2_WinUI3_Sample (Package) | Packaging and distribution project | +| WebView2_WinUI3_Sample (Package)\FixedRuntime | (Optional) Fixed WebView2 runtime | +| WebView2_WinUI3_Sample (Package)\FixedRuntime\95.0.1020.53 | (Optional) Fixed WebView2 runtime sample | + + +# Fixed version usage +If you want to ship a fixed version of the WebView2 runtime with your application you will need to include it in your project. + +Instructions can be found at: https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution + +The following assume you are using runtime version 95.0.1020.53, you will change this number to whatever version you are using. + +You will need to: +1 Include the fixed WebView2 runtime in the package project +
\WebView2_WinUI3_Sample\WebView2_WinUI3_Sample (Package)\FixedRuntime\95.0.1020.53\
+2 Update the package project wapproj file for the version your using +
 < Content Include="FixedRuntime\95.0.1020.53\\**\*.*" > 
+3 Uncomment the code in app.xaml.cs to enable the runtime override +
+public App()
+{
+    this.InitializeComponent();
+    // If you are shipping a fixed version WebView2 SDK with your application you will need
+    // to use the following code (update the runtime version to what you are shipping.
+    StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
+    String fixedPath = Path.Combine(localFolder.Path, "FixedRuntime\\95.0.1020.53");
+    Debug.WriteLine($"Launch path [{localFolder.Path}]");
+    Debug.WriteLine($"FixedRuntime path [{fixedPath}]");
+    Environment.SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", fixedPath);
+}
+
+4 Update the version information for the appropriate version +
 
+String fixedPath = Path.Combine(localFolder.Path, "FixedRuntime\\95.0.1020.53");
+
+ -This sample (**WebView2_WinUI3_Sample**) demonstrates using a WebView2 control in a WinUI 3 (Windows App SDK) Packaged application. -![Sample app](./images/sample-app.png) -This sample also allows you to ship the app with a fixed-version WebView2 Runtime, instead of using whichever version of the WebView2 Runtime is installed and running on the user's computer. -To use this sample, see [WinUI 3 (Windows App SDK) sample app](https://learn.microsoft.com/microsoft-edge/webview2/samples/webview2-winui3-sample).