Bài viết
Kho tài liệu và bài viết được chia sẻ, đánh giá bởi cộng đồng
Nội dung bài viết
Dẫn Nhập
Giao tiếp giữa các Windows trong WPF
Nội Dung
Để đọc hiểu bài này tốt nhất các bạn nên có kiến thức cơ bản về các phần:
Trong bài viết này, chúng ta sẽ cùng đi qua các phần:
- Làm thế nào để giao tiếp giữa các Windows trong WPF?
- Chọn giải pháp.
- Triển khai và thực hiện.
Vấn đề chính
Trên thực tế khi làm việc với WPF chúng ta sẽ gặp không ít trường hợp như, người dùng kích hoạt sự kiện Click On Button ở Window 1 và chúng ta mong muốn các Window khác cũng nhận được sự kiện tương tự, để xử lý các nghiệp vụ liên quan.
Chọn giải pháp
Đối với các vấn đề giao tiếp giữa các Windows thì mình sẽ chọn cách sử dụng Event để chúng có thể giao tiếp với nhau, tất nhiên là có thể có nhiều cách khác nhưng sử dụng Event thì sẽ phù hợp cho các mô hình như MVVM và 3 Layer.
Triển khai và thực hiện
Mình sẽ tạo ra hai Windows để làm ví dụ, Window thứ nhất sẽ hiển thị thời gian xảy ra sự kiện Click On Button của Window thứ hai, cùng xem ví dụ bên dưới nhé.
Ý tưởng của mình là khi Window thứ hai Click thì Window thứ nhất sẽ nhận được thời gian xảy ra sự kiện của Window thứ hai, vì vậy mình sẽ tạo Window thứ hai trước bởi vì nó là Publisher nơi xảy ra sự kiện, dịch sát nghĩa xíu là người phát hành sự kiện.
MyWindow.xaml (Window thứ hai)
<Window x:Class="EventAppWPF.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EventAppWPF"
mc:Ignorable="d"
Title="MyWindow" Height="450" Width="800">
<Grid>
<Button x:Name="myButton" Width="79" Height="29" Click="myButton_Click"/>
</Grid>
</Window>
MyWindow.cs (Window thứ hai)
Vì mình muốn gửi dữ liệu đến các nơi đăng ký nhận sự kiện nên mình phải tạo thêm Class CustomEventArgs để truyền dữ liệu.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace EventAppWPF
{
/// <summary>
/// Interaction logic for MyWindow.xaml
/// </summary>
public partial class MyWindow : Window
{
public event EventHandler<CustomEventArgs> RaiseCustomEvent;
public MyWindow() => InitializeComponent();
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
EventHandler<CustomEventArgs> raiseEvent = RaiseCustomEvent;
raiseEvent.Invoke(this, e);
}
private void myButton_Click(object sender, RoutedEventArgs e) => OnRaiseCustomEvent(new CustomEventArgs("Event triggered"));
}
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string message) => Message = message;
public string Message { get; set; }
}
}
MainWinodw.xaml (Window thứ nhất)
<Window x:Class="EventAppWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EventAppWPF"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400">
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Window 2 Clicked At Time:"/>
<TextBlock x:Name="Tb_time" Margin="5 0 0 0" FontWeight="Medium"/>
</StackPanel>
</Grid>
</Window>
MainWindow.cs (Window thứ nhất)
Window này sẽ là nơi đăng ký sự nhận sự kiện của Window thứ 2, hay có thể nói nó là Subscriber dịch sát nghĩa thì là người đăng ký nhận sự kiện.
using System;
using System.Windows;
namespace EventAppWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyWindow myWindow = new MyWindow();
myWindow.Show();
myWindow.RaiseCustomEvent += MyWindow_RaiseCustomEvent;
}
private void MyWindow_RaiseCustomEvent(object sender, CustomEventArgs e) => Tb_time.Text = DateTime.Now.ToString();
}
}
Nội dung bài viết