【问题标题】:want to access data from text box in the form which is in another solution in visual studio 2013?想要以 Visual Studio 2013 中另一个解决方案的形式从文本框中访问数据?
【发布时间】:2015-07-01 21:47:58
【问题描述】:

我有两个解决方案 TransferService 和 Sender。 TranferService 具有 WCF 服务和 IISHost 来托管该服务。在发件人解决方案中,我有 Windows 窗体应用程序。在该表单中,我使用按钮浏览和选择文件,使用文本框显示所选文件路径,使用另一个按钮(发送)通过 WCF 服务传输该文件。但我无法访问传输解决方案中的文本框值。它显示“当前上下文中不存在该名称”。
TransferService 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TransferService" in both code and config file together.
public class TransferService : ITransferService
{

    public File DownloadDocument()
    {
        File file = new File();
        String path = txtSelectFilePath.Text;
        file.Content = System.IO.File.ReadAllBytes(@path);
        //file.Name = "ImagingDevices.exe";

        return file;
    }
}
}

我在这条线上遇到错误

String path = txtSelectFilePath.Text;

form.cs 的代码

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sender
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Browse_Click(object sender, EventArgs e)
    {              
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            txtSelectFilePath.Text = openFileDialog1.FileName;

        }

    }

    private void Send_Click(object sender, EventArgs e)
    {
        TransferService.TransferServiceClient client = new TransferService.TransferServiceClient();
        TransferService.File file = client.DownloadDocument();
        System.IO.File.WriteAllBytes(@"C:\DownloadedFiles\" + file.Name, file.Content);
        MessageBox.Show(file.Name + " is downloaded");
    } 


}
}

ITransferService.cs 的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITransferService" in both code and config file together.
[ServiceContract]
public interface ITransferService
{
    [OperationContract]
    File DownloadDocument();
}

[DataContract]
public class File
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public byte[] Content { get; set; }

}
}

提前多谢了…………

【问题讨论】:

  • 如果我正确理解你的问题:你不能。
  • 我认为最好在 DownloadDocument(string path) 中添加一个字符串参数,并在调用时传递路径
  • 您的回复......乔治先生,当我试图传递参数时,这个错误来了:-'TransferService.TransferService'没有实现接口成员'TransferService.ITransferService.DownloadDocument()'
  • 当我创建 WCF 服务并实现该接口时,它生成了没有参数的函数。是这个原因吗??
  • 是的,但我认为你可以通过创建一个构造函数来克服这个问题......用代码查看我的 awnser

标签: c# winforms wcf


【解决方案1】:

然后为您的类创建一个构造函数,该构造函数以字符串形式接收路径,如下所示:

public class TransferService : ITransferService
{

    private string _path;
    public TransferService(string path) {
        _path  = path
    }

    public File DownloadDocument()
    {
        File file = new File();
        //String path = txtSelectFilePath.Text;
        file.Content = System.IO.File.ReadAllBytes(_path);
        //file.Name = "ImagingDevices.exe";

        return file;
    }
}

然后在 form.cs 上

 TransferService.TransferServiceClient client = new TransferService.TransferServiceClient(txtSelectFilePath.Text);

【讨论】:

  • TransferService.File 文件 = client.DownloadDocument(txtSelectFilePath.Text);当我尝试这个时,它给了我错误,比如方法 DownloadDocument 没有过载。
  • 是否也需要编辑ITransferService.cs这个文件...?
  • 抱歉已修复,不,我创建它是为了实现接口而无需更改它,但由您决定
猜你喜欢
  • 2013-09-28
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 2015-10-13
  • 2015-10-10
  • 1970-01-01
  • 2013-11-25
相关资源
最近更新 更多