从WCF服务设置WPF控制

本文关键字:WPF 控制 设置 服务 WCF | 更新日期: 2023-09-27 18:08:54

我正在尝试使用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;
    }
}

从WCF服务设置WPF控制

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

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

"类视图模型{私有静态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);
        }

在服务契约类中做了类似的更改。我希望这将帮助一些人试图获得

中的值

不要使用静态属性,因为你不能绑定到它们。使用静态对象,或者将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");
        }
    }    
}