【问题标题】:WPF Databinding problem of a WCF [MessageContract] classWCF [MessageContract] 类的 WPF 数据绑定问题
【发布时间】:2009-07-21 13:53:53
【问题描述】:

我在服务器上有一堂课

   [MessageContract]
public class RemoteFileInfo : IDisposable, INotifyPropertyChanged
{
    [MessageHeader(MustUnderstand = true)]
    public string _FileName;

    [MessageHeader(MustUnderstand = true)]
    public string FileName
    {
        get { return _FileName; }
        set { _FileName = value; }
    }


    [MessageBodyMember(Order = 1)]
    public System.IO.Stream _FileByteStream;

    [MessageHeader(MustUnderstand = true)]
    public System.IO.Stream FileByteStream
    {
        get { return _FileByteStream; }
        set { _FileByteStream = value; }
    }

    public void Dispose()
    {
        // close stream when the contract instance is disposed. 
        // this ensures that stream is closed when file download 
        // is complete, since download procedure is handled by the client 
        // and the stream must be closed on server.
        if (_FileByteStream!=null)
        {
            _FileByteStream.Close();
            _FileByteStream = null;
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

我通过 WCF 与我的服务器通信。这个类应该是 [MessageContract] 因为流式传输 - (我所有的其他类都使用 [DataContract])。

我的问题是,在客户端使用 SvcUtil 生成类后,我丢失了 INotify... 接口...我明白了:

    [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="RemoteFileInfo", WrapperNamespace="http://tempuri.org/", IsWrapped=true)]
public partial class RemoteFileInfo
{
    [System.ServiceModel.MessageHeaderAttribute(Namespace="http://tempuri.org/")]
    public System.IO.Stream FileByteStream;

    [System.ServiceModel.MessageHeaderAttribute(Namespace="http://tempuri.org/")]
    public string FileName;

    [System.ServiceModel.MessageHeaderAttribute(Namespace="http://tempuri.org/")]
    public string _FileName;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=0)]
    public System.IO.Stream _FileByteStream;

    public RemoteFileInfo()
    {
    }

    public RemoteFileInfo(
                System.IO.Stream FileByteStream, 
                string FileName, 
                string _FileName, 
                System.IO.Stream _FileByteStream)
    {
        this.FileByteStream = FileByteStream;
        this.FileName = FileName;
        this._FileName = _FileName;
        this._FileByteStream = _FileByteStream;
    }
}

如果没有 INotifyPropertyChanged,我知道数据绑定不起作用,但我可以像这样在客户端添加它:

    public partial class RemoteFileInfo : System.ComponentModel.INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    #endregion
}

但数据绑定仍然不起作用。你有什么主意吗? 我根本无法将数据绑定与 [MessageContract] 类一起使用???

提前感谢您的回答!

【问题讨论】:

    标签: c# wpf wcf data-binding


    【解决方案1】:

    消息合约只指定消息的结构。服务器端出现的任何接口都不会出现在客户端,因为它们与消息的结构无关。

    此外,如果您的客户端是用 Java 编写的,它会如何处理这些接口?

    【讨论】:

    • 好的。感谢您的快速答复!我想,但现在我知道了:)。但是我仍然不明白为什么当我尝试在本地添加接口时无法正确绑定 - 正如您在我的笔记最后一部分看到的那样。你知道吗?
    • 但是你从来没有提出过这个事件,是吗?我不知道 WPF,但肯定需要一些代码来引发该事件,否则 WPF 将不知道更改。
    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多