2015-04-22 74 views
1

我使用WPF進行數據綁定時遇到問題。我有一個Web服務(WCF)到一個Windows服務應用程序和一個WPF應用程序來控制服務。在WPF應用程序中,我建立了一個文本框,我想從WebService接收日誌。WPF DataBinding與其他命名空間/類

此時我可以從同一個命名空間(WPF應用程序)發送新數據,但是當我使用數據類的實例從(WCF應用程序)發送它時,它不反映文本框中的新數據。

這裏是我的代碼:

MainWindow.xaml

... 
    <Grid Name="grid" Margin="0,0,346.6,4"> 
     <TextBox Name="Log" Text="{Binding Path=LogText}" ScrollViewer.CanContentScroll="True" IsReadOnly="True" BorderThickness="0" Background="Transparent" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Column="2" Margin="30.8,35,-325.8,0" Height="303" Grid.RowSpan="2" Width="295"/> 
    </Grid> 
... 

MainWindow.xaml.cs

public MainWindow() 
    { 
       InitializeComponent(); 
       grid.DataContext = Logs.Instance; 
       ... 
    } 

public class Logs : INotifyPropertyChanged 
{ 
      private static Logs instance; 

      private Logs() { } 

      public static Logs Instance 
      { 
       get 
       { 
       if (instance == null) 
       { 
        instance = new Logs(); 
       } 
       return instance; 
       } 
      } 

      public event PropertyChangedEventHandler PropertyChanged; 

      protected void Notify(string propName) 
      { 
       if (this.PropertyChanged != null) 
       { 
        PropertyChanged(this,new PropertyChangedEventArgs(propName)); 
       } 
      } 

      private string _LogText = ""; 

      public string LogText 
      { 
       get 
       { 
        return _LogText; 
       } 
       set 
       { 
        _LogText = value; 
        Notify("LogText"); 
       } 
      } 

      public void LogBinding(String text) 
      { 
       LogText = text + LogText; 
      } 
} 

WCF web服務發送文本呼叫(其它命名空間)

Using "THE NAMESPACE OF WPF APP"; 

Logs.Instance.LogBinding("Some Text"); 

THANK YOU!

回答

1

從您的描述中可以看出,您有兩個單獨的應用程序,它們作爲單獨的進程運行。靜態實例不會跨進程共享,即使它們是相同的類。您需要使用某種形式的跨進程通信將數據從Windows服務傳遞到WPF應用程序。

+0

你是對的!我的應用程序運行在不同的進程中!謝謝,你清楚我的想法。所以你推薦的溝通方式:類似命名管道的東西? – Fernando

+0

是的,命名管道可以成爲這種情況的好方法。 –