从WCF服务设置WPF控制

时间:2014-06-17 14:10:14

标签: c# .net wpf wcf xaml

我正在尝试使用WCF服务设置名称(文本框)值。我在WPF应用程序中托管服务。我最初使用MVVM模型来设置MainWindow.cs中的文本框值并且它有效。但后来我做了一些静态属性,以便通过服务合同访问相同的属性。它似乎仍然设置了Model属性的属性,但没有在文本框中更改值。有人可以指导我吗?

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;

    }
}

2 个答案:

答案 0 :(得分:1)

感谢Nathan的帮助。以下是答案:

我将ViewModel更改为Singleton类,并在创建实例时实例化了复合Model对象。

`类ViewModel     {         private static volatile ViewModel实例;         私有静态对象_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);
        }

在服务合同类中进行了类似的更改。我希望这会帮助一些人试图获得

中的价值

答案 1 :(得分: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");
        }
    }    
}