728x90
반응형
728x170
■ Dispatcher 클래스의 Run 정적 메소드를 사용해 다중 윈도우 다중 스레드를 사용하는 방법을 보여준다. 이 메소드는 새 스레드의 시작점이다. 이 스레드의 제어하에 새 창을 만든다. WPF는 새 스레드를 관리하기 위해 새 Dispatcher를 자동으로 만든다. 창을 기능적으로 만들기 위해 해야 할 일은 Dispatcher를 시작하는 것뿐이다.
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="TestProject"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"
Orientation="Horizontal">
<Button Name="newWindowButton"
Width="100"
Height="30"
Content="New Window" />
<TextBox Name="newLocationTextBox"
Margin="10 0 0 0"
Width="500"
BorderThickness="1"
BorderBrush="Black"
VerticalContentAlignment="Center" />
<Button Name="browseButton"
Margin="10 0 0 0"
Width="100"
Height="30"
Content="Browse" />
</StackPanel>
<Frame Name="frame" Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="0 10 0 0"
BorderThickness="1"
BorderBrush="Black"
NavigationUIVisibility="Hidden"
Source="https://www.daum.net" />
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Threading;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Loaded += Window_Loaded;
this.newWindowButton.Click += newWindowButton_Click;
this.browseButton.Click += browseButton_Click;
this.frame.Navigated += frame_Navigated;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)
/// <summary>
/// 윈도우 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.frame.Source = new Uri("https://www.daum.net");
}
#endregion
#region New Window 버튼 클릭시 처리하기 - newWindowButton_Click(sender, e)
/// <summary>
/// New Window 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void newWindowButton_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(ProcessNewWindow);
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
#endregion
#region Browse 버튼 클릭시 처리하기 - browseButton_Click(sender, e)
/// <summary>
/// Browse 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void browseButton_Click(object sender, RoutedEventArgs e)
{
this.frame.Source = new Uri(this.newLocationTextBox.Text);
}
#endregion
#region 프레임 이동 완료시 처리하기 - frame_Navigated(sender, e)
/// <summary>
/// 프레임 이동 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void frame_Navigated(object sender, NavigationEventArgs e)
{
NavigationService navigationService = this.frame.NavigationService;
dynamic browser = navigationService.GetType().GetField("_webBrowser", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(navigationService);
dynamic iWebBrowser2 = browser.GetType().GetField("_axIWebBrowser2", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(browser);
iWebBrowser2.Silent = true;
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 새 윈도우 처리하기 - ProcessNewWindow()
/// <summary>
/// 새 윈도우 처리하기
/// </summary>
private void ProcessNewWindow()
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
Dispatcher.Run();
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] LengthConverter 클래스 : ConvertFromString 메소드를 사용해 문자열에서 길이 구하기 (0) | 2023.01.09 |
---|---|
[C#/WPF] BulletDecorator 엘리먼트 사용하기 (0) | 2023.01.09 |
[C#/WPF] Border 클래스 : BorderThickness 속성을 변경시키는 애니메이션 만들기 (0) | 2023.01.09 |
[C#/WPF] ControlTemplate 엘리먼트 : 버튼 배경색을 그라데이션 처리한 Button 엘리먼트 정의하기 (0) | 2023.01.08 |
[C#/WPF] Frame 클래스 : Navigated 이벤트를 사용해 스크립트 오류 억제하기 (0) | 2023.01.08 |
[C#/WPF] 백그라운드 스레드로 차단 작업 처리하기 (0) | 2023.01.08 |
[C#/WPF] DispatcherObject 클래스 : Dispatcher 속성을 사용해 장기 실행 계산이 포함된 단일 스레드 애플리케이션 만들기 (0) | 2023.01.07 |
[C#/WPF] ObjectCache 클래스 : 응용 프로그램 데이터 캐싱하기 (0) | 2023.01.06 |
[C#/WPF] RenderOptions 클래스 : SetCachingHint 정적 메소드를 사용해 캐싱 힌트 옵션 설정하기 (0) | 2023.01.04 |
[C#/WPF] HwndHost 클래스 : Win32 컨트롤 호스트하기 (0) | 2023.01.04 |
댓글을 달아 주세요