Skip to content

Commit e23e8dc

Browse files
authored
Merge pull request #107 from MicrosoftEdge/user/DaShoe/publishuwpsample
uwp sample
2 parents a71b5c7 + 5b3625a commit e23e8dc

19 files changed

+815
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
bin/
2+
.vs/
3+
obj/
4+
# Created by running event monitor
5+
enc_temp_folder/
6+
packages/
7+
8+
# Override root .gitignore to ensure we pick up changes for the sample app
9+
!*.sln
10+
!*.csproj
11+
12+
# Make sure script debug config is checked in for vendor testing
13+
!.vscode
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!--Copyright (C) Microsoft Corporation. All rights reserved.
2+
Use of this source code is governed by a BSD-style license that can be
3+
found in the LICENSE file.-->
4+
5+
<Application
6+
x:Class="webview2_sample_uwp.App"
7+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
8+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9+
xmlns:local="using:webview2_sample_uwp">
10+
<Application.Resources>
11+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
12+
</Application.Resources>
13+
</Application>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Runtime.InteropServices.WindowsRuntime;
10+
using Windows.ApplicationModel;
11+
using Windows.ApplicationModel.Activation;
12+
using Windows.Foundation;
13+
using Windows.Foundation.Collections;
14+
using Windows.UI.Xaml;
15+
using Windows.UI.Xaml.Controls;
16+
using Windows.UI.Xaml.Controls.Primitives;
17+
using Windows.UI.Xaml.Data;
18+
using Windows.UI.Xaml.Input;
19+
using Windows.UI.Xaml.Media;
20+
using Windows.UI.Xaml.Navigation;
21+
22+
namespace webview2_sample_uwp
23+
{
24+
/// <summary>
25+
/// Provides application-specific behavior to supplement the default Application class.
26+
/// </summary>
27+
sealed partial class App : Application
28+
{
29+
/// <summary>
30+
/// Initializes the singleton application object. This is the first line of authored code
31+
/// executed, and as such is the logical equivalent of main() or WinMain().
32+
/// </summary>
33+
public App()
34+
{
35+
this.InitializeComponent();
36+
this.Suspending += OnSuspending;
37+
}
38+
39+
/// <summary>
40+
/// Invoked when the application is launched normally by the end user. Other entry points
41+
/// will be used such as when the application is launched to open a specific file.
42+
/// </summary>
43+
/// <param name="e">Details about the launch request and process.</param>
44+
protected override void OnLaunched(LaunchActivatedEventArgs e)
45+
{
46+
Frame rootFrame = Window.Current.Content as Frame;
47+
48+
// Do not repeat app initialization when the Window already has content,
49+
// just ensure that the window is active
50+
if (rootFrame == null)
51+
{
52+
// Create a Frame to act as the navigation context and navigate to the first page
53+
rootFrame = new Frame();
54+
55+
rootFrame.NavigationFailed += OnNavigationFailed;
56+
57+
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
58+
{
59+
//TODO: Load state from previously suspended application
60+
}
61+
62+
// Place the frame in the current Window
63+
Window.Current.Content = rootFrame;
64+
}
65+
66+
if (e.PrelaunchActivated == false)
67+
{
68+
if (rootFrame.Content == null)
69+
{
70+
// When the navigation stack isn't restored navigate to the first page,
71+
// configuring the new page by passing required information as a navigation
72+
// parameter
73+
rootFrame.Navigate(typeof(MainPage), e.Arguments);
74+
}
75+
// Ensure the current window is active
76+
Window.Current.Activate();
77+
}
78+
}
79+
80+
/// <summary>
81+
/// Invoked when Navigation to a certain page fails
82+
/// </summary>
83+
/// <param name="sender">The Frame which failed navigation</param>
84+
/// <param name="e">Details about the navigation failure</param>
85+
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
86+
{
87+
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
88+
}
89+
90+
/// <summary>
91+
/// Invoked when application execution is being suspended. Application state is saved
92+
/// without knowing whether the application will be terminated or resumed with the contents
93+
/// of memory still intact.
94+
/// </summary>
95+
/// <param name="sender">The source of the suspend request.</param>
96+
/// <param name="e">Details about the suspend request.</param>
97+
private void OnSuspending(object sender, SuspendingEventArgs e)
98+
{
99+
var deferral = e.SuspendingOperation.GetDeferral();
100+
//TODO: Save application state and stop any background activity
101+
deferral.Complete();
102+
}
103+
}
104+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!--Copyright (C) Microsoft Corporation. All rights reserved.
2+
Use of this source code is governed by a BSD-style license that can be
3+
found in the LICENSE file.-->
4+
5+
<Page
6+
x:Class="webview2_sample_uwp.MainPage"
7+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
8+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9+
xmlns:local="using:webview2_sample_uwp"
10+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
11+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
12+
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
13+
mc:Ignorable="d"
14+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
15+
16+
<Grid>
17+
<Grid.RowDefinitions>
18+
<RowDefinition Height="50"/>
19+
<RowDefinition Height="*"/>
20+
<RowDefinition Height="50"/>
21+
</Grid.RowDefinitions>
22+
23+
<Grid Grid.Row="0" Background="LightGray">
24+
<Grid.ColumnDefinitions>
25+
<ColumnDefinition Width="*"/>
26+
<ColumnDefinition Width="50"/>
27+
</Grid.ColumnDefinitions>
28+
<TextBox Grid.Column="0" x:Name="AddressBar" KeyDown="AddressBar_KeyDown" VerticalAlignment="Center" Margin="10,0,0,0"/>
29+
<Button Grid.Column="1" x:Name="Go" Content="Go" Click="Go_OnClick" VerticalAlignment="Center" HorizontalAlignment="Center"/>
30+
</Grid>
31+
32+
<controls:WebView2 x:Name="WebView2" Grid.Row="1"/>
33+
34+
<Rectangle Grid.Row="2" Fill="LightGray"/>
35+
<TextBlock x:Name="StatusBar" Text="WebView2" VerticalAlignment="Center" Grid.Row="2" Margin="10,0,10,0"/>
36+
</Grid>
37+
</Page>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma warning disable CS8305 //'Microsoft.UI.Xaml.Controls.WebView2' is for evaluation purposes only and is subject to change or removal in future updates.
6+
7+
using Microsoft.UI.Xaml.Controls;
8+
using Microsoft.Web.WebView2.Core;
9+
using System;
10+
using System.Diagnostics;
11+
using Windows.UI.Xaml;
12+
using Windows.UI.Xaml.Controls;
13+
using Windows.UI.Xaml.Input;
14+
15+
namespace webview2_sample_uwp
16+
{
17+
public sealed partial class MainPage : Page
18+
{
19+
public MainPage()
20+
{
21+
this.InitializeComponent();
22+
AddressBar.Text = "https://developer.microsoft.com/en-us/microsoft-edge/webview2/";
23+
WebView2.Source = new Uri(AddressBar.Text);
24+
25+
WebView2.NavigationCompleted += WebView2_NavigationCompleted;
26+
27+
StatusUpdate("Ready");
28+
}
29+
30+
private void StatusUpdate(string message)
31+
{
32+
StatusBar.Text = message;
33+
Debug.WriteLine(message);
34+
}
35+
36+
private void WebView2_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
37+
{
38+
StatusUpdate("Navigation complete");
39+
}
40+
41+
private bool TryCreateUri(String potentialUri, out Uri result)
42+
{
43+
StatusUpdate("TryCreateUri");
44+
45+
Uri uri;
46+
if ((Uri.TryCreate(potentialUri, UriKind.Absolute, out uri) || Uri.TryCreate("http://" + potentialUri, UriKind.Absolute, out uri)) &&
47+
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
48+
{
49+
result = uri;
50+
return true;
51+
}
52+
else
53+
{
54+
StatusUpdate("Unable to configure URI");
55+
result = null;
56+
return false;
57+
}
58+
}
59+
60+
private void TryNavigate()
61+
{
62+
StatusUpdate("TryNavigate");
63+
64+
Uri destinationUri;
65+
if (TryCreateUri(AddressBar.Text, out destinationUri))
66+
{
67+
WebView2.Source = destinationUri;
68+
}
69+
else
70+
{
71+
StatusUpdate("URI couldn't be figured out use it as a bing search term");
72+
73+
String bingString = "https://www.bing.com/search?q=" + Uri.EscapeUriString(AddressBar.Text);
74+
if (TryCreateUri(bingString, out destinationUri))
75+
{
76+
AddressBar.Text = destinationUri.AbsoluteUri;
77+
WebView2.Source = destinationUri;
78+
}
79+
else
80+
{
81+
StatusUpdate("URI couldn't be configured as bing search term, giving up");
82+
}
83+
}
84+
}
85+
86+
private void Go_OnClick(object sender, RoutedEventArgs e)
87+
{
88+
StatusUpdate("Go_OnClick: " + AddressBar.Text);
89+
90+
TryNavigate();
91+
}
92+
93+
private void AddressBar_KeyDown(object sender, KeyRoutedEventArgs e)
94+
{
95+
if (e.Key == Windows.System.VirtualKey.Enter)
96+
{
97+
StatusUpdate("AddressBar_KeyDown [Enter]: " + AddressBar.Text);
98+
99+
e.Handled = true;
100+
TryNavigate();
101+
}
102+
}
103+
}
104+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Package
4+
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
5+
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
6+
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
7+
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
8+
IgnorableNamespaces="uap mp rescap">
9+
10+
<Identity
11+
Name="WebView2-WinUI2-UWP-Sample"
12+
Publisher="CN=Microsoft"
13+
Version="1.0.0.0" />
14+
15+
<mp:PhoneIdentity PhoneProductId="b4dc306d-3ab0-42e9-91b5-fc497299ae26" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
16+
17+
<Properties>
18+
<DisplayName>WebView2 WinUI2 UWP Sample</DisplayName>
19+
<PublisherDisplayName>Microsoft</PublisherDisplayName>
20+
<Logo>Assets\StoreLogo.png</Logo>
21+
</Properties>
22+
23+
<Dependencies>
24+
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
25+
</Dependencies>
26+
27+
<Resources>
28+
<Resource Language="x-generate"/>
29+
</Resources>
30+
31+
<Applications>
32+
<Application Id="App"
33+
Executable="$targetnametoken$.exe"
34+
EntryPoint="WebView2_UWP.App">
35+
<uap:VisualElements
36+
DisplayName="WebView2 WinUI2 UWP Sample"
37+
Square150x150Logo="Assets\Square150x150Logo.png"
38+
Square44x44Logo="Assets\Square44x44Logo.png"
39+
Description="WebView2_UWP sample application"
40+
BackgroundColor="transparent">
41+
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
42+
<uap:SplashScreen Image="Assets\SplashScreen.png" />
43+
</uap:VisualElements>
44+
</Application>
45+
</Applications>
46+
47+
<Capabilities>
48+
<Capability Name="internetClient" />
49+
<Capability Name="internetClientServer" />
50+
<Capability Name="privateNetworkClientServer" />
51+
<uap:Capability Name="enterpriseAuthentication"/>
52+
<uap:Capability Name="sharedUserCertificates"/>
53+
<rescap:Capability Name="enterpriseCloudSSO" />
54+
<DeviceCapability Name="location"/>
55+
<DeviceCapability Name="microphone"/>
56+
<DeviceCapability Name="webcam"/>
57+
</Capabilities>
58+
</Package>

0 commit comments

Comments
 (0)