【问题标题】:Setting WPF Control from WCF Service从 WCF 服务设置 WPF 控件
【发布时间】:2014-06-17 14:10:14
【问题描述】:

我正在尝试使用 WCF 服务设置名称(文本框)值。我在 WPF 应用程序中托管服务。我最初使用 MVVM 模型从 MainWindow.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

 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 客户端设置值。
  • 这是您的问题的一部分。您将它们设为静态并创建新实例并在其上设置值。但这不会改变您创建的原始模型实例上的任何值。相反,为您的 非静态 模型创建一个静态单例,并从您的 WCF 代码中修改它。
  • 我将 ViewModel 更改为单例类,现在我使用以下 try { ViewModel s = ViewModel.Instance; 在 MainWindow.xaml.cs 中访问它this.DataContext = s.NameValue.TestName; s.NameValue.TestName = "艾丽西亚"; this.TextName.DataContext = s; } catch (Exception e) { MessageBox.Show("Error" + e.Message); }
  • 它给了我错误“对象引用未设置为对象的实例”,尽管通过调用跟踪我可以看到它正在达到我正在创建 ViewModel 的单个实例的地步。 if (instance == null) { lock (_mutex) { if (instance == null) { instance = new ViewModel(); } } } 返回实例; } }

标签: c# .net wpf wcf xaml


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    不要使用静态属性,因为您无法绑定到它们。改用静态对象或将模型对象传递给服务,例如在构造函数中,并使用该实例进行更新。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多