簡體   English   中英

在Visual Studio 2015中引用另一個項目

[英]Referencing another project in Visual Studio 2015

這是我有2個C#Windows窗體項目項目A和B的情況。項目A有一個文本框和一個按鈕。 當按下按鈕時,文本框中的任何值都會保存在另一個類中。 我們將其稱為ClassA ,它將通過ClassA.myString = textbox.Text;保存ClassA.myString = textbox.Text;

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。 任何幫助表示贊賞。 謝謝。

如果這些項目在不同的進程中運行,則必須使用一種機制進行進程間通信,請查看此鏈接http://weblogs.asp.net/ricardoperes/local-machine-interprocess-communication-網

如果您可以在另一個項目中使用一個項目的組件,但又不能將它們作為兩個單獨的可執行文件運行,那么這很簡單。 在這里,我假設您正在使用WPF,但是您可以將類似技術應用於WinForms或您使用的任何其他框架:

應用程式A:

<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中輸入文本,單擊“保存”,然后在App B中單擊“加載”。您會注意到App A將被凍結,直到您在App B中閱讀文本為止。說,這並不意味着作為生產應用程序,而僅僅是概念的證明。 我保證它會做錯事情-純粹是作為示例繼續進行。

應用程式A:

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 它們更加強大,值得學習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM