通过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
最满意答案
这不是客户端验证问题。 你的模型有一个十进制类型的字段? 模型联编程序会尝试绑定一个价值$ 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。 将其验证为只接受货币输入的正则表达式的字符串。 它可能是您可以用于您的
BudgetText
的set
方法的相同正则表达式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
'sset
method
相关问答
更多-
asp.net MVC3 ?[2023-05-13]
mvc就是模型(Model),视图(View),控制器(Controller)加上路由控制(routing),用户请求的url到routing,然后由routing规则找到对应的controller里的action,再由action调用model处理数据,然后返回给对应的view!个人觉得mvc和三层没多大区别,只是代码更清晰,分离的更好,更灵活一点!vs2010已经可以建立asp.net mvc应用程序了。自己建一个看下 -
我会这样做的方式是创建一个自定义属性,它扩展了DataType属性和一个自定义的html助手。 这并不一定是最简单的方法,但它会在未来节省时间。 编辑公司CultureInfo.CreateSpecificCulture(cultureName)而不是switch 自定义属性 public class CurrencyDisplayAttribute : DataTypeAttribute { public string Culture { get; set; } public Curre ...
-
Swift / Xcode - 向UITextField添加货币符号和逗号(Swift/Xcode - Add a currency symbol and commas to UITextField)[2022-11-07]
您可以通过将分数位设置为0来摆脱浮点数 formatter.maximumFractionDigits = 0 formatter.minimumFractionDigits = 0 用英镑符号得到结果 formatter.currencySymbol = "£" 并没有任何迹象得到结果 formatter.currencySymbol = "" You can get rid of floating points by setting fraction digits ... -
您可以使用prefix选项更改符号。 以下是我以两种不同方式执行此操作的代码片段:修改货币别名并定义自己的别名。 在你的版本中,你不能输入任何东西,因为mask属性用于限制输入并将其设置为0.00只允许输入这四个字符,而不是其他任何东西。 9.99的面具将允许一个数字,然后是一个句号和两个数字。 9有一个特殊的掩码定义允许任何数字。 Inputmask.extendAliases({ pesos: { prefix: "₱ ", groupSepar ...
-
这不是客户端验证问题。 你的模型有一个十进制类型的字段? 模型联编程序会尝试绑定一个价值$ 123,456.78的值并失败,因此该值将为空。 以下是解决此问题的一种方法: 将模型更改为具有掩码小数点的字符串属性: public decimal? Budget { get; set; } public string BudgetText { get { return Budget.HasValue ? Budget.ToString("$") : string.Empty; } ...
-
Modelstate.Remove("yourkey"); //remove one Modelstate.Clear(); //remove all Modelstate.Remove("yourkey"); //remove one Modelstate.Clear(); //remove all
-
你可以使用CSS来为你的下拉菜单设置自定义的附件: .customDrowDown { -webkit-appearance: none; -moz-appearance: none; // add background image what you like } 并将此课程添加到您的下拉列表中: @Html.DropDownList("DropDownID", String.Empty, new {@class="customDrowDown "} ) You can use css to set ...
-
只需将空字符串作为第二个参数传递给过滤函数即可。 value = $filter('currency')(value, ""); 你也可以注入currencyFilter本身。 您不必从过滤器工厂中派生出来,只需执行以下操作: value = currencyFilter(value, ""); 注意语法 : $ filter('currency')(amount,symbol,fractionSize) Just pass empty string as second argument to th ...
-
您可以更改文化中的货币符号。 创建一个文化的副本,然后改变任何你想要的: var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); culture.NumberFormat.CurrencySymbol = "€"; @String.Format(culture, "{0:C}", 12); 也许你可以保留一些静态变量的文化副本,所以不需要克隆每个请求... You can change the currency symbol in th ...
-
那么使用HttpRuntime.Cache,这个例子会缓存一个小时? HttpRuntime.Cache.Add("SchoolSettings", someObject, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, ...