2015-04-22 72 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

是的,命名管道可以成为这种情况的好方法。 –