2016-03-07 193 views
0

這裏是場景 我有2個C#窗體項目,項目A和項目B.項目A有一個文本框和一個按鈕。當按下按鈕時,文本框中的任何值都會保存在另一個類中。我們將這稱爲ClassA,它通過ClassA.myString = textbox.Text;保存。在Visual Studio 2015中引用另一個項目

public class ClassA 
{ 
    public String myString 
    { 
     get; 
     set; 
    } 
} 

現在項目B有一個按鈕和一個標籤。當按下按鈕時,它應該將標籤設置爲項目A中保存到ClassA中的值。我已經通過右鍵單擊項目,單擊添加,引用並指向項目B中的項目A來建立引用。我使用ProjectA;在我的項目B表單中,但我無法獲得價值。以下是我嘗試失敗的一種方法。

using ProjectA; 

namespace projectBSolution 
{ 
    public class ProjectB 
    { 
     ClassA myClass; 
     public ProjectB() 
     { 
      InitializeComponent(); 
      myClass = new ClassA(); 
     } 
     private void btn_click(object sender, EventArgs e) 
     { 
     label1.Text = myClass.myString; 
     } 
    } 
} 

問題是這不會返回我的值,因爲我正在初始化該類的新版本。如果我不初始化一個新版本,它每次都會返回null。任何幫助表示讚賞。謝謝。

+1

這些是在單獨的進程中運行的兩個單獨的可執行文件是否正確?如果這是真的,那麼讓一個項目引用另一個項目根本沒有幫助,你需要的是某種進程間通信。 – CodingGorilla

+2

我認爲你誤解了引用意味着什麼 - 引用只是允許一個程序集「查看」另一個程序集的公共API。這並不意味着兩者之間有任何類型的數據傳輸......你並不是在這裏「保存」任何值......如果你能解釋你在概念上而不是技術上想要做的事情概念也許我們可以用更有用的方式引導你 – Charleh

+0

這就是我所害怕的一種。基本目標是使項目B能夠監視項目A中的字符串值,並在項目A更改時顯示它們。它們是單獨的可執行文件。 –

回答

0

如果您能夠接受一個項目使用組件在另一個內部,但不能將它們作爲兩個單獨的可執行文件運行,那麼這是一件非常簡單的事情。在這裏,我假設你使用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; 
     } 
    } 
} 

理想情況下,將有一個環或保持的輸入數據流的內容軌道,而不是一個事件按下按鈕即可獲取。

在此選項的基礎上,您還有很多其他很棒的選項,如WCFMSMQ。他們更強大,值得學習。

+1

謝謝你的回答,這正是我所期待的。當談到C#編程和使用Visual Studio時,我幾乎是一個初學者,這是我第一次使用多個項目。這個答案給了我所需要的所有信息,讓它工作。謝謝。 –

+0

@PatrickHansen沒問題;很高興我能幫上忙。 –

相關問題