首页 \ 问答 \ 通过C#API调用Windows窗体应用程序(Calling a Windows Forms Application via C# API)

通过C#API调用Windows窗体应用程序(Calling a Windows Forms Application via C# API)

我有一个用C#编写的Windows窗体应用程序,它允许我指向一个图像文件夹,并将这些图像解析为一个易于查看的格式供用户使用。 这是一个独立的应用程序,它工作正常。 我想扩展这个应用程序,以便它不是解析图像文件夹,而是可以将它预先设置为数据集,其中所有图像和元数据都由外部应用程序通过API预设。

我已经能够将应用程序编译到类库中并访问类和结构以实际构建数据集而没有问题,我现在遇到的问题是使用我构建的数据集在外部启动应用程序。

对于上下文,我正在编写一个工具,允许用户从Spotfire调用此Windows窗体应用程序。 在spotfire里面,我正在解析一个DataTable对象,并根据我的信息构建数据集。 构建此数据集之后,我需要能够将应用程序作为独立进程启动,而不是在Spotfire内部显式调用表单。 (这是由于Spotfire中GUI线程的限制,我们无法在单线程应用程序中调用多线程进程,并且我们希望保持Spotfire GUI响应,如果我们直接调用表单,则无法执行)

我知道我可以使用Process.Start()启动exe独立程序,但这不允许我将我的信息传递给应用程序。 如何构建此应用程序以允许我将信息传递给它? 我一直试图谷歌如何做到这一点的例子,并继续空手而归,因为人们将参考ASP.net或我头脑中的事情。

先谢谢你!

编辑:下面是一个处理这个问题的应用程序的一个例子。 我们使用DPlot Jr在外部创建图形。 dplot.dll公开以下函数:

[System.Runtime.InteropServices.DllImport("dplotlib64.dll")]
public static extern int DPlot_Plot8(
    ref DPLOT d, double[] x, double[] y, string cmds);

然后我可以在我的代码中使用它

docNum = dplot.DPlot_Plot8(ref dPlot, X, Y, cmds);
docNums.Add(docNum);

以这种方式调用此函数实际上启动了dplot应用程序并将我构建的对象“dPlot”与X和Y数据一起传递以绘制信息。 我想在我的Windows窗体应用程序中构建类似的东西,以便能够从外部应用程序轻松启动它。 不幸的是我不知道这个函数在.dll中是如何工作的

EDIT2:我已经能够通过Aybe建议的命令行修改运行时。 在我的桌面应用程序中,我在主程序中创建了一个conditonal就像这样。

        if (args.Length == 0 && false)
        {
            Application.Run(new frmWaveFormViewer());
        }
        else
        {
            DataSet dataSet = new DataSet();
            //dataSet.LoadMetaData(args[0]);
            dataSet.LoadMetaData(@"C:\Users\a0273881\AppData\Local\Temp\tmp1141.tmp");
            Application.Run(new frmWaveFormViewer(dataSet));
        }

然后,用户可以使用以下内容在外部调用表单...

            DataSet dataSet = new DataSet(dpList);

            dataSet.PrintDatasetMetadataToFile(String.Format(@"C:\Spotfire\DataSetTesting_{0}.csv", DateTime.Now.ToString("yyyyMMdd_HHmmss")));
            string args = dataSet.GetMetaData();

            ProcessStartInfo starter = new ProcessStartInfo();
            starter.FileName = @"C:\Users\a0273881\Desktop\WaveFormViewer.exe";
            starter.Arguments = args;
            Process.Start(starter);

但是,这对其他开发人员来说并不容易。

我开始研究WCF,任何人都可以在WCF上为傻瓜提供良好的资源吗? 我目前正在阅读: http//www.codemag.com/article/0705041


I have a windows form application written in C# that allows me to point to a folder of images and parse those images into a easily view able format for a user. This is a stand alone application and it works fine. I want to extend this application so that instead of it parsing a folder of images, I can hand it a prebuilt data set, with all of the images and meta data preset by an external application via an API.

I have been able to compile the application into a class library and access the classes and structs to actually build the data set without issue, the problem I am having now is launching the application externally with the data set I have built.

For context, I am writing a tool that will allow the user to call this windows form application from Spotfire. Inside of spotfire I am parsing a DataTable object and building the data set from the information I have. Once this data set is built I need to be able to launch the application as a stand-alone process instead of calling the forms explicitly inside of Spotfire. (This is due to a limitation of GUI threads in Spotfire and we can't call a multi threaded process in a single threaded application as well as we want to keep the Spotfire GUI responsive which can't be done if we call the forms directly)

I know I can launch the exe standalone using Process.Start(), however this doesn't let me pass my information to teh application. How can I build this application to allow me to pass information to it? I've been trying to google examples of how to do this and keep coming up empty handed as people will reference ASP.net or things that are over my head.

Thank you in advance!

EDIT: An example of an application that handles this really well is below. We use DPlot Jr to create graphs externally. The dplot.dll exposes the following function:

[System.Runtime.InteropServices.DllImport("dplotlib64.dll")]
public static extern int DPlot_Plot8(
    ref DPLOT d, double[] x, double[] y, string cmds);

which I can then use in my code

docNum = dplot.DPlot_Plot8(ref dPlot, X, Y, cmds);
docNums.Add(docNum);

calling this function in this way actually launches the dplot application and passes the object I've built "dPlot" along with the X and Y data in order to plot the information. I would like to build something like this in my windows form application in order to be able to launch it easily from an external application. Unfortunately I don't know how this function works inside the .dll

EDIT2: I have been able to modify the runtime via the commandline as suggested by Aybe. In my desktop application I have created a conditonal in the main program like so.

        if (args.Length == 0 && false)
        {
            Application.Run(new frmWaveFormViewer());
        }
        else
        {
            DataSet dataSet = new DataSet();
            //dataSet.LoadMetaData(args[0]);
            dataSet.LoadMetaData(@"C:\Users\a0273881\AppData\Local\Temp\tmp1141.tmp");
            Application.Run(new frmWaveFormViewer(dataSet));
        }

the user can then call the forms externally by using the following...

            DataSet dataSet = new DataSet(dpList);

            dataSet.PrintDatasetMetadataToFile(String.Format(@"C:\Spotfire\DataSetTesting_{0}.csv", DateTime.Now.ToString("yyyyMMdd_HHmmss")));
            string args = dataSet.GetMetaData();

            ProcessStartInfo starter = new ProcessStartInfo();
            starter.FileName = @"C:\Users\a0273881\Desktop\WaveFormViewer.exe";
            starter.Arguments = args;
            Process.Start(starter);

However, this is not easy to use for other developers.

I am starting to look into WCF, can anyone provide good resources on WCF for dummies? I'm currently reading through: http://www.codemag.com/article/0705041


原文:https://stackoverflow.com/questions/37756784
更新时间:2023-07-03 09:07

最满意答案

这不是客户端验证问题。 你的模型有一个十进制类型的字段? 模型联编程序会尝试绑定一个价值$ 123,456.78的值并失败,因此该值将为空。 以下是解决此问题的一种方法:

将模型更改为具有掩码小数点的字符串属性:

public decimal? Budget { get; set; }
public string BudgetText {
    get {
        return Budget.HasValue ? Budget.ToString("$") : string.Empty;
    }
    set {
        // parse "value" and try to put it into Budget
    }
}

然后,从您的View中绑定到BudgetText。 将其验证为只接受货币输入的正则表达式的字符串。 它可能是您可以用于您的BudgetTextset方法的相同正则表达式


This isn't a client side validation problem. Your model has a field of type decimal? The model binder will try to bind a value of $123,456.78 into that and fail, so the value will be null. Here's one way to get around this:

Change your model to have a string property that masks your decimal:

public decimal? Budget { get; set; }
public string BudgetText {
    get {
        return Budget.HasValue ? Budget.ToString("$") : string.Empty;
    }
    set {
        // parse "value" and try to put it into Budget
    }
}

Then, just bind to BudgetText from your View. Validate it as a string with a regular expression that accepts only money input. It'll probably be the same regex you can use for your BudgetText's set method

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)