QT的信号、槽机制优雅地实现了数据传递与动作激发,C#没有对应方法。现在用Event实现一下。(后面补充了用更方便的Action实现)
Page1
<Page x:Class="TestMyEvent.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestMyEvent"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<Button Content="Send" HorizontalAlignment="Left" Margin="57,48,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<TextBox x:Name="tb" HorizontalAlignment="Left" Margin="57,79,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
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.Navigation;
using System.Windows.Shapes;
namespace TestMyEvent
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
public static event EventHandler<MyEventArgs>? Signal;
private void Button_Click(object sender, RoutedEventArgs e)
{
Signal?.Invoke(this, new MyEventArgs() {Str = tb.Text});
}
}
public class MyEventArgs : EventArgs
{
public String? Str { get; set; }
}
}
Page2
<Page x:Class="TestMyEvent.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestMyEvent"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page2" Background="LightBlue">
<Grid>
<TextBox x:Name="tb" HorizontalAlignment="Left" Margin="50,38,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Navigation;
using System.Windows.Shapes;
namespace TestMyEvent
{
/// <summary>
/// Page2.xaml 的交互逻辑
/// </summary>
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
Page1.Signal += Slot; //相当于Qt的Connnect
}
public void Slot(object? sender, MyEventArgs e)
{
tb.Text = e.Str;
}
}
}
MainWindow
<Window x:Class="TestMyEvent.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:TestMyEvent"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Frame Source="page1.xaml"></Frame>
<Frame Grid.Column="1" Source="page2.xaml"></Frame>
</Grid>
</Window>
总结:虽不中,不远矣。
补充:经过学习,发现用Action代替EventHandler是更简便的方法,不必自定义EventArgs。
比如上例中可以这样定义Signal:
public static Action<String>? Signal;
省掉了不必要的参数.