■ WebView2 클래스에서 웹 페이지에서 HTML 본문의 텍스트를 클립보드에 복사하드는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Border Grid.Row="0" BorderThickness="1" BorderBrush="DarkGray"> <WebView2 Name="webView2" /> </Border> <StackPanel Name="controlStackPanel" Grid.Row="2" Orientation="Horizontal" Spacing="10"> <TextBlock Name="urlTextBlock" VerticalAlignment="Center" Text="주소" /> <TextBox Name="urlTextBox" VerticalAlignment="Center" Width="300" Text="https://icodebroker.com" /> <Button Name="navigateButton" VerticalAlignment="Center" Padding="5" Content="이동하기" /> <Button Name="copyTextButton" VerticalAlignment="Center" Padding="5" Content="텍스트 복사" /> </StackPanel> </Grid> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
using System; using System.Threading.Tasks; using Windows.ApplicationModel.DataTransfer; using Microsoft.UI; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Microsoft.Web.WebView2.Core; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 텍스트 박스 디폴트 배경 브러시 /// </summary> private Brush textBoxDefaultBackgroundBrush; /// <summary> /// 텍스트 박스 성공시 배경 브러시 /// </summary> private SolidColorBrush textBoxSuccessBackgroundBrush = new SolidColorBrush(Colors.Green); /// <summary> /// 텍스트 박스 실패시 배경 브러시 /// </summary> private SolidColorBrush textBoxFailureBackgroundBrush = new SolidColorBrush(Colors.Red); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.textBoxDefaultBackgroundBrush = this.urlTextBox.Background; Loaded += Page_Loaded; Unloaded += Page_Unloaded; this.navigateButton.Click += navigateButton_Click; this.copyTextButton.Click += copyTextButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 페이지 로드시 처리하기 - Page_Loaded(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void Page_Loaded(object sender, RoutedEventArgs e) { Loaded -= Page_Loaded; await this.webView2.EnsureCoreWebView2Async(); this.webView2.CoreWebView2.NavigationStarting += coreWebView2_NavigationStarting; this.webView2.CoreWebView2.NavigationCompleted += coreWebView2_NavigationCompleted; this.webView2.Source = new Uri(this.urlTextBox.Text.Trim()); } #endregion #region 페이지 언로드시 처리하기 - Page_Unloaded(sender, e) /// <summary> /// 페이지 언로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Page_Unloaded(object sender, RoutedEventArgs e) { if(this.webView2 != null) { this.webView2.Close(); this.webView2 = null; } } #endregion #region 코어 웹뷰 2 탐색 시작시 처리하기 - coreWebView2_NavigationStarting(sender, e) /// <summary> /// 코어 웹뷰 2 탐색 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void coreWebView2_NavigationStarting(CoreWebView2 sender, CoreWebView2NavigationStartingEventArgs e) { this.urlTextBox.IsEnabled = false; this.navigateButton.IsEnabled = false; this.copyTextButton.IsEnabled = false; } #endregion #region 코어 웹뷰 2 탐색 완료시 처리하기 - coreWebView2_NavigationCompleted(sender, e) /// <summary> /// 코어 웹뷰 2 탐색 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void coreWebView2_NavigationCompleted(CoreWebView2 sender, CoreWebView2NavigationCompletedEventArgs e) { if(e.IsSuccess) { this.urlTextBox.Background = this.textBoxSuccessBackgroundBrush; } else { this.urlTextBox.Background = this.textBoxFailureBackgroundBrush; } this.urlTextBox.IsEnabled = true; this.navigateButton.IsEnabled = true; this.copyTextButton.IsEnabled = true; DispatcherQueue.TryEnqueue ( async () => { await Task.Delay(500); this.urlTextBox.Background = this.textBoxDefaultBackgroundBrush; this.urlTextBox.Focus(FocusState.Programmatic); this.urlTextBox.SelectAll(); } ); } #endregion #region 탐색 버튼 클릭시 처리하기 - navigateButton_Click(sender, e) /// <summary> /// 탐색 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void navigateButton_Click(object sender, RoutedEventArgs e) { this.webView2.Source = new Uri(this.urlTextBox.Text.Trim()); } #endregion #region 텍스트 복사 버튼 클릭시 처리하기 - copyTextButton_Click(sender, e) /// <summary> /// 텍스트 복사 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void copyTextButton_Click(object sender, RoutedEventArgs e) { string pageText = await this.webView2.CoreWebView2.ExecuteScriptAsync("document.body.innerText"); pageText = pageText.Trim('"').Replace("\\n", "\n").Replace("\\r", "\r"); DataPackage dataPackage = new DataPackage(); dataPackage.SetText(pageText); Clipboard.SetContent(dataPackage); } #endregion } |