简体   繁体   English

从WCF服务设置WPF控制

[英]Setting WPF Control from WCF Service

I am trying to set the name (textbox) value using WCF Service. 我正在尝试使用WCF服务设置名称(文本框)值。 I am hosting service in WPF application. 我在WPF应用程序中托管服务。 I used the MVVM Model initially to set textbox value from the MainWindow.cs and it worked. 我最初使用MVVM模型从MainWindow.cs设置文本框值,并且它起作用了。 But then I made some properties static in order to access the same through the service contract. 但是后来我将一些属性设为静态,以便通过服务合同访问它们。 It still seems to setting the property of Model attribute but not changing value in the text box. 似乎仍然可以设置Model属性的属性,但不能在文本框中更改值。 Can anyone please guide me? 谁能指导我?

Model.cs 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 WCF InameService.cs

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

    }


}

MainWindow.xaml 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 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;

    }
}

Thanks Nathan for help. 感谢内森(Nathan)的帮助。 Following is the answer: 以下是答案:

I changed the ViewModel to Singleton Class and also instantiated the composite Model object while creating the instance. 我将ViewModel更改为Singleton Class,并在创建实例时实例化了复合Model对象。

`class ViewModel { private static volatile ViewModel instance; `class ViewModel {私有静态volatile ViewModel实例; private static object _mutex = new 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;
        }
    }
}`

then changed the MainWindow.xaml.cs 然后更改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);
        }

Similar changes was done in the Service Contract Class. 在服务合同类中进行了类似的更改。 I hope this will help some one trying to get the value in 我希望这会帮助一些试图获得价值的人

Don't use static properties as you can't bind to them. 不要使用静态属性,因为它们无法绑定到它们。 Use a static object instead or pass the Model object to the service for example in the constructor and use that instance for updates. 可以使用静态对象代替,也可以将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");
        }
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM