c# - 同一台计算机中两个不同 WPF 应用程序之间的通信
问题描述
我有两个 WPF 应用程序(Application1 和 Application2),Application1 将用户添加到Users.xml文件中,Application2 将 Users.xml 中的所有名称显示到标签中。要刷新 Application2 中的标签,我必须在当前实现中按刷新按钮。我想建立一种机制,以便每当我使用 Application1 添加用户时,Application2 都会自动更新标签。我可以实现这一点的一种方法是每当 Application1 添加用户时,它都会在 XML 文件中设置一些标志(例如IsSync=false) 和 Application2 持续监视标志,每当它看到 IsSync=false 时,它会更新标签并设置 IsSync=true。但我想知道是否有任何其他最佳方式(可能是发布者/订阅者方式,通过从 Application1 处理 Application2 的 Refresh 按钮)来实现这一点。你能帮我实现这个目标吗?我在下面附上了 XAML/代码:
应用1
XAML
<Window x:Class="Application1.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:Application1"
mc:Ignorable="d"
Title="Application1" Height="500" Width="500">
<Grid>
<Label Name="lblUserName" Content="Name" HorizontalAlignment="Left" Margin="42,60,0,0" VerticalAlignment="Top"/>
<TextBox Name="txtUserName" HorizontalAlignment="Left" Height="23" Margin="91,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="300"/>
<Button Name="btnAdd" Content="Add" HorizontalAlignment="Left" Margin="310,110,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
代码隐藏
using System.IO;
using System.Windows;
using System.Xml;
using System.Xml.Linq;
namespace Application1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static string fullPath = "C:\\Files\\Users.xml";
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtUserName.Text))
{
if (!File.Exists(fullPath))
{
// If Users.xml is not exists, create a file and add Textbox Name to the file
CreateUsersXMLAndAddUser();
txtUserName.Text = "";
}
else
{
// Add Textbox name to the Users.xml
AddUser();
txtUserName.Text = "";
}
}
else
{
MessageBox.Show(this, "Name can not be empty", "Required", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void AddUser()
{
XElement xEle = XElement.Load(fullPath);
xEle.Add(new XElement("User", new XAttribute("Name", txtUserName.Text.Trim())));
xEle.Save(fullPath);
}
private void CreateUsersXMLAndAddUser()
{
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Users",
new XElement("User",
new XAttribute("Name", txtUserName.Text.Trim())
)));
StringWriter sw = new StringWriter();
XmlWriter xWrite = XmlWriter.Create(sw);
xDoc.Save(xWrite);
xWrite.Close();
xDoc.Save(fullPath);
}
}
}
应用2
XAML
<Window x:Class="Application2.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:Application2"
mc:Ignorable="d"
Title="Application2" Height="500" Width="600">
<Grid>
<Label Name="lblUsers" FontSize="20" FontWeight="UltraBold" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top"/>
<Button Name="btnRefreshUsers" Content="Refresh" FontSize="20" FontWeight="UltraBold" HorizontalAlignment="Left" Margin="450,39,0,0" VerticalAlignment="Top" Height="100" Width="100" Click="btnRefreshUsers_Click"/>
</Grid>
</Window>
代码隐藏
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Xml.Linq;
namespace Application2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static string fullPath = "C:\\Files\\Users.xml";
StringBuilder userList;
public MainWindow()
{
InitializeComponent();
userList = new StringBuilder();
lblUsers.Content = string.Empty;
lblUsers.Content = LoadUsers();
}
private StringBuilder LoadUsers()
{
userList.AppendLine(" Users ");
userList.AppendLine("---------");
if (File.Exists(fullPath))
{
XElement xelement = XElement.Load(fullPath);
IEnumerable<XElement> users = xelement.Elements();
foreach (var user in users)
{
userList.AppendLine(user.Attribute("Name").Value);
}
}
else
{
userList.AppendLine("Nothing to show ...");
}
return userList;
}
private void btnRefreshUsers_Click(object sender, RoutedEventArgs e)
{
userList.Clear();
lblUsers.Content = string.Empty;
lblUsers.Content = LoadUsers();
}
}
}
解决方案
进程间通信有多种方式:
- MSMQ(微软消息队列)
- 套接字编程
- 命名管道
- Web 服务(WCF,...)
从理论上讲,在低级别的通信中,大多数此类技术都使用套接字作为主要部分。所以套接字编程是低级通信,你有更多的控制权,你需要做更多的事情才能让它工作。
我已经阅读了一个很好的答案:
推荐阅读
- sql - 使用和不使用新换行符合并和计数字符串
- javascript - 如何在 Vuex 中观察状态变化?
- swift - 斯威夫特:我能知道一个函数是否是一个方法吗?如果是这样,我能知道它的对象吗?
- class - 如何在 intellij jar 构建中包含 .class 文件
- recursion - 没有参数和静态变量的递归
- sql - 在两个表中查询 Postgres
- anaconda - 将 Conda 环境移动到无法访问 Internet 的计算机
- css - 我可以在 NativeScript 应用程序的深色模式下使用白色背景吗?
- javascript - 如何通过单击状态从 FlatList 中一一删除项目
- powershell - 使用 Mailkit 和 Powershell 在 Imap 文件夹中搜索未读电子邮件