2014-06-17 86 views
0

我想使用WCF服務設置名稱(文本框)的值。我在WPF應用程序中託管服務。我最初使用MVVM模型來設置MainWindow.cs中的文本框值,並且它工作正常。但後來我做了一些靜態屬性,以便通過服務合同訪問相同的屬性。它似乎仍然設置了模型屬性的屬性,但沒有改變文本框中的值。任何人都可以請指導我?從WCF服務設置WPF控件

Model.cs

public class Model : INotifyPropertyChanged 
{ 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    protected bool SetField<T>(ref T field, T value, string propertyName) 
    { 
     if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
     field = value; 
     OnPropertyChanged(propertyName); 
     MessageBox.Show(field.ToString()); 

     return true; 
    } 

    // props 
    private static string testname; 
    public static string TestName 
    { 
     get { return testname; } 
     set { 
      Model m = new Model(); 
      m.SetField(ref testname, value, "TestName"); 
     } 
    }  


} 

WCF InameService.cs

public class nameService : InameService 
{ 
    public void setMyName(string name) 
    { 
     Model.TestName = name; 

    } 


} 

MainWindow.xaml

<Grid Name="GridName"> 

    <TextBox Name="TextName" HorizontalAlignment="Left" Height="23" Margin="193,140,0,0" TextWrapping="Wrap" Text="{Binding TestName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" /> 

</Grid> 

個MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     ServiceHost host = new ServiceHost(typeof(nameService)); 
     InitializeComponent(); 
     host.Open(); 

     Model s = new Model(); 
     //this.DataContext = s.NameValue.TestName; 
     Model.TestName = "Alicia"; 
     this.TextName.DataContext = s; 

    } 
} 
+0

首先,不要以爲你可以使任何屬性靜態,並期望它仍然工作。它不會。 –

+0

如果我公開發布,則無法通過服務運營合同確定價值。我只能使用按鈕點擊等任何觸發器從服務契約中獲取值,但無法使用我需要的WCF客戶端設置值。 – user3585570

+0

這是你問題的一部分。您將它們設置爲靜態並創建新實例併爲其設置值。但是這並不會改變您創建的原始模型實例的任何值。相反,請爲您的**非靜態**模型創建一個靜態單例,並從您的WCF代碼中修改它。 –

回答

1

感謝Nathan的幫助。以下是答案:

我將ViewModel更改爲Singleton類,並在創建實例的同時還實例化了組合模型對象。

`class ViewModel { private static volatile ViewModel instance; private static object _mutex = new object();

private ViewModel() { } 


    private Model model;   

    public Model NameValue 
    { 
     get { return model; } 
     set { model = value; } 
    }   


    public static ViewModel Instance 
    { 
     get 
     { 
      if (instance == null) 
      { 
       lock (_mutex) 
       { 
        if (instance == null) 
        { 
         instance = new ViewModel(); 
         instance.model = new Model(); 
        } 
       } 
      } 

      return instance; 
     } 
    } 
}` 

再變MainWindow.xaml.cs

try 
     { 
      ViewModel s = ViewModel.Instance; 

      s.NameValue.TestName = "Alicia"; 
      this.DataContext = s; 
      this.TextName.DataContext = s; 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show("Error" + e.Message); 
     } 

類似的變化在服務合同類已完成。我希望這會幫助一些人試圖獲得價值

0

,你不能與他們不要使用靜態屬性。例如,使用靜態對象或將Model對象傳遞給服務,例如在構造函數中使用該實例進行更新。

public class nameService : InameService 
{ 

    private Model model; 

    public nameService(Model m) 
    { 
     model = m; 
    } 

    public void setMyName(string name) 
    { 
     model.TestName = name; 
    } 
} 

public class Model : INotifyPropertyChanged 
{ 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    protected bool SetField<T>(ref T field, T value, string propertyName) 
    { 
     if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
     field = value; 
     OnPropertyChanged(propertyName); 
     MessageBox.Show(field.ToString()); 

     return true; 
    } 

    // props 
    private string testname; 
    public string TestName 
    { 
     get { return testname; } 
     set { 
      Model m = new Model(); 
      m.SetField(ref testname, value, "TestName"); 
     } 
    }  
}