如果您能夠接受一個項目使用組件在另一個內部,但不能將它們作爲兩個單獨的可執行文件運行,那麼這是一件非常簡單的事情。在這裏,我假設你使用WPF,但你可以將類似技術的WinForms,或者你使用任何其他的框架:
應用答:
<Window x:Class="SampleApp.A.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:SampleApp.A"
mc:Ignorable="d"
Title="A"
SizeToContent="WidthAndHeight">
<StackPanel>
<TextBox x:Name="textBox" VerticalAlignment="Center"
Width="250" Margin="10" Height="30"/>
<Button Content="Save" Width="80" Margin="10" Click="Button_Click"/>
</StackPanel>
</Window>
using System.Windows;
namespace SampleApp.A
{
public partial class MainWindow : Window
{
public string Text { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Text = textBox.Text;
}
}
}
應用B:
<Window x:Class="SampleApp.B.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:SampleApp.B"
mc:Ignorable="d"
Title="B"
SizeToContent="WidthAndHeight">
<StackPanel>
<Label x:Name="label" VerticalAlignment="Center"
Width="250" Margin="10" Height="30"/>
<Button Content="Load" Width="80" Margin="10" Click="Button_Click"/>
</StackPanel>
</Window>
using System.Windows;
namespace SampleApp.B
{
public partial class MainWindow : Window
{
private A.MainWindow A;
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
A = new A.MainWindow();
A.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
label.Content = A.Text;
}
}
}
請注意,我正在儘可能地說明這一點。理想情況下,您可以使用INotifyPropertyChanged
,MVVM和一大堆其他設計技術和模式,這些在這個例子中顯然不存在。
這個例子顯示的是,你可以讓一個項目從另一個項目啓動一個窗口並訪問它的公共屬性。當然,這不是兩個可執行文件交互。如果單獨的可執行文件是您所要的目標,那麼這些選項會有點棘手,它們從Interop窗口句柄到消息傳遞以及它們之間的許多事物都有所不同。
例如,您可以使用IO.Pipes來處理您的消息傳遞例程。下面是一個例子,但它非常粗糙,缺乏大量的安全檢查,並可能會在幾分鐘的使用後崩潰(我沒有時間測試,因爲我正在出發)。它與上面的例子類似 - 你在App A中輸入文本,點擊Save,然後點擊App B中的Load。你會注意到App A將被凍結,直到你閱讀App B中的文本。就像我說,這不是一個生產應用程序,而只是一個概念證明。我保證它會做出錯誤的 - 這純粹是作爲一個例子來建立。
應用程式:
using System.IO;
using System.IO.Pipes;
using System.Windows;
namespace SampleApp.A
{
public partial class MainWindow : Window
{
private NamedPipeClientStream pipeClient;
public string Text { get; set; }
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.Closing += MainWindow_Closing;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
pipeClient = new NamedPipeClientStream(".", "1234", PipeDirection.Out);
pipeClient.Connect();
}
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
pipeClient?.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Text = textBox.Text;
using (var sw = new StreamWriter(pipeClient))
{
sw.WriteLine(Text);
sw.Flush();
}
}
}
}
應用B:
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Threading.Tasks;
using System.Windows;
namespace SampleApp.B
{
public partial class MainWindow : Window
{
private NamedPipeServerStream pipeServer;
public string Text { get; set; }
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.Closing += MainWindow_Closing;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Process.Start(@"C:\Users\bk\Desktop\SampleApp V2\SampleApp.A\bin\Debug\SampleApp.A.exe");
pipeServer = new NamedPipeServerStream("1234", PipeDirection.In);
pipeServer.WaitForConnection();
}
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (pipeServer.IsConnected)
{
pipeServer.Disconnect();
}
pipeServer.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (pipeServer.IsConnected)
{
using (var sr = new StreamReader(pipeServer))
{
Text = sr.ReadLine();
}
}
label.Content = Text;
}
}
}
理想情況下,將有一個環或保持的輸入數據流的內容軌道,而不是一個事件按下按鈕即可獲取。
在此選項的基礎上,您還有很多其他很棒的選項,如WCF和MSMQ。他們更強大,值得學習。
這些是在單獨的進程中運行的兩個單獨的可執行文件是否正確?如果這是真的,那麼讓一個項目引用另一個項目根本沒有幫助,你需要的是某種進程間通信。 – CodingGorilla
我認爲你誤解了引用意味着什麼 - 引用只是允許一個程序集「查看」另一個程序集的公共API。這並不意味着兩者之間有任何類型的數據傳輸......你並不是在這裏「保存」任何值......如果你能解釋你在概念上而不是技術上想要做的事情概念也許我們可以用更有用的方式引導你 – Charleh
這就是我所害怕的一種。基本目標是使項目B能夠監視項目A中的字符串值,並在項目A更改時顯示它們。它們是單獨的可執行文件。 –