简体   繁体   中英

Setting WPF Control from WCF Service

I am trying to set the name (textbox) value using WCF Service. I am hosting service in WPF application. I used the MVVM Model initially to set textbox value from the MainWindow.cs and it worked. 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. Can anyone please guide me?

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;

    }
}

Thanks Nathan for help. Following is the answer:

I changed the ViewModel to Singleton Class and also instantiated the composite Model object while creating the instance.

`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;
        }
    }
}`

then changed the 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.

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");
        }
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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