从vb转换为c# [英] Convert from vb to c#

查看:88
本文介绍了从vb转换为c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Salem对所有人来说;

i正在搜索关于打印和灯光开关的报道,我确实找到了这个解决方案:

http://blogs.msdn.com/b /bethmassi/archive/2010/09/10/using-microsoft-word-to-create-reports-for-lightswitch-or-silverlight.aspx [ ^ ]



但问题是解决方案是在vb中我没有将其转换为c#,这是我要转换它的代码:< br $>


 Imports System.Runtime.InteropServices.Automation 
Imports < xmlns:ns0 = urn:microsoft:ordermanager :customer >

命名空间LightSwitchApplication
模块MyReports

Public Sub RunCustomerReportFixedTemplate (ByVal cust As Customer)
If AutomationFactory.IsAvailable Then
Try
'动态地从我们的实体属性创建XML数据
Dim myXML = < customer >
<% =来自prop in cust.Details.Properties.All
选择<<%= prop.Name.ToLower %> > <% = if(prop.Value, - %> < / >
%>
< / customer >

使用word = AutomationFactory.CreateObject(Word.Application)
Dim resourceInfo = System.Windows.Application.GetResourceStream(New Uri(CustomerDetails.docx,UriKind.Relative) ))
Dim fileName = CopyStreamToTempFile(resourceInfo.Stream,。docx)

Dim doc = word.Documents.Open(fileName)
'抓住现有的绑定自定义XML在doc
Dim customXMLPart = doc.CustomXMLParts(urn:microsoft:ordermanager:customer)

Dim all = customXMLPart.SelectSingleNode(// *)
Dim replaceNode = customXMLPart.SelectSingleNode(/ ns0:root [1] / customer [1])

'替换< customer > 节点带有这个新数据的XML
all.ReplaceChildSubtree(myXML.ToString,replaceNode)

word.Visible = True

结束使用
Catch ex As Exception
抛出新的InvalidOperationException(无法创建客户报告。,ex)
结束尝试
结束如果
结束Sub

Public Sub RunCustomerReportDynamicTemplate(ByVal cust as Customer)
如果AutomationFactory.IsAvailable那么

尝试
Dim templateFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+
\ Reportl \ CustomerDetails.docx

'从我们的enti创建XML数据动态的ty属性
Dim myXML = < ns0:root >
< customer >
<% =来自prop In cust.Details.Properties.All
选择<< ;%= prop.Name.ToLower %> > <% = If(prop.Value , - %> < / >
%>
< / customer >
< / ns0:root >

使用word = AutomationFactory.CreateObject(Word.Application)

Dim tempFile = GetTempFileName(。docx)
File .Copy(templateFile,tempFile)

Dim doc = word.Documents.Open(tempFile)

'将新的自定义XML部件添加到文档
Dim customXMLPart = doc.CustomXMLParts.Add(myXML.ToString())

'绑定我们根据控件的标题找到的任何内容控件
For i = 1 to doc.ContentControls.Count
Dim ctrl = doc.ContentControls(i)

如果不是ctrl.XMLMapping.IsMapped那么

ctrl.XMLMapping.SetMapping(/ ns0:root [1] / customer [ 1] /+ ctrl.Title.ToString.ToLower(),
xmlns:ns0 =urn:microsoft:ordermanager:customer,customXMLPart)
End if
Next

word.Visible = True
结束使用
Catch ex As Exception
抛出新的InvalidOperationException(无法创建客户报告。,ex)
结束尝试
结束如果
结束子

私有函数CopyStreamToTempFile(ByVal stream As System.IO.Stream,ByVal ext As String)As String
Dim path = GetTempFileName (分机)
'创建临时文件
Dim file = Sys tem.IO.File.Create(path)
file.Close()
'将流写入磁盘
使用fileStream = System.IO.File.Open(path,
System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Write,
System.IO.FileShare.None)

Dim buffer(0 To stream.Length - 1 )By Byte
stream.Read(buffer,0,stream.Length)
fileStream.Write(buffer,0,buffer.Length)

fileStream.Close()
结束使用
返回路径
结束函数

私有函数GetTempFileName(ByVal ext As String)As String
'在My Documents中返回一个uinuqe文件名基于guid
Dim路径的报告= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +\Reports
如果不是Directory.Exists(路径)那么
Directory.CreateDirectory(路径)
结束如果

Dim filename = Guid.NewGuid( ).ToString()& ext
path = System.IO.Path.Combine(path,filename)

返回路径
结束函数
结束模块

结束命名空间











这一切class我没有正确转换它也没有在线转换器也没有正确转换,

特别是这部分:



 Dim myXML = <   customer  >  
<% =来自prop In cust.Details.Properties.All
选择<<%= prop.Name.ToLower %> > <% = if(prop.Value, - %> < / >
%>
< / customer >





对此有任何帮助???

解决方案

否每当你需要在这些语言之间进行翻译时,需要提出这样的问题。您可以随时自动完成。

请查看我过去的答案:

C#对VB.NET的COde行解释 [ ^ ],

需要将vb代码转换为c# [ ^ ],

FixedPage to ContentPage将c#代码转换为vb.net [ ^ ],

来自exe的源代码 [ ^ ]。



特别注意使用开源ILSpy的最强大的离线方法。它提供高质量的翻译。



-SA


在这里:


点击我! [ ^ ]


vb to c#和c#to vb


Salem to all;
i was searching about printing and repoting in light switch and i did found this solution :
http://blogs.msdn.com/b/bethmassi/archive/2010/09/10/using-microsoft-word-to-create-reports-for-lightswitch-or-silverlight.aspx[^]

but the problem is the solution is in vb and i did not convert it to c# , this is the code that i want to convert it:

Imports System.Runtime.InteropServices.Automation
Imports <xmlns:ns0="urn:microsoft:ordermanager:customer">

Namespace LightSwitchApplication
    Module MyReports

        Public Sub RunCustomerReportFixedTemplate(ByVal cust As Customer)
            If AutomationFactory.IsAvailable Then
                Try
                    'Create the XML data from our entity properties dynamically
                    Dim myXML = <customer>
                                    <%= From prop In cust.Details.Properties.All
                                        Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
                                    %>
                                </customer>

                    Using word = AutomationFactory.CreateObject("Word.Application")
                        Dim resourceInfo = System.Windows.Application.GetResourceStream(New Uri("CustomerDetails.docx", UriKind.Relative))
                        Dim fileName = CopyStreamToTempFile(resourceInfo.Stream, ".docx")

                        Dim doc = word.Documents.Open(fileName)
                        'Grab the existing bound custom XML in the doc
                        Dim customXMLPart = doc.CustomXMLParts("urn:microsoft:ordermanager:customer")

                        Dim all = customXMLPart.SelectSingleNode("//*")
                        Dim replaceNode = customXMLPart.SelectSingleNode("/ns0:root[1]/customer[1]")

                        'replace the <customer> node in the existing custom XML with this new data
                        all.ReplaceChildSubtree(myXML.ToString, replaceNode)

                        word.Visible = True

                    End Using
                Catch ex As Exception
                    Throw New InvalidOperationException("Failed to create customer report.", ex)
                End Try
            End If
        End Sub

        Public Sub RunCustomerReportDynamicTemplate(ByVal cust As Customer)
            If AutomationFactory.IsAvailable Then

                Try
                    Dim templateFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                                       "\Reports\CustomerDetails.docx"

                    'Create the XML data from our entity properties dynamically
                    Dim myXML = <ns0:root>
                                    <customer>
                                        <%= From prop In cust.Details.Properties.All
                                            Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
                                        %>
                                    </customer>
                                </ns0:root>

                    Using word = AutomationFactory.CreateObject("Word.Application")

                        Dim tempFile = GetTempFileName(".docx")
                        File.Copy(templateFile, tempFile)

                        Dim doc = word.Documents.Open(tempFile)

                        'Add the new custom XML part to the document
                        Dim customXMLPart = doc.CustomXMLParts.Add(myXML.ToString())

                        'bind any content controls that we find based on the title of the control
                        For i = 1 To doc.ContentControls.Count
                            Dim ctrl = doc.ContentControls(i)

                            If Not ctrl.XMLMapping.IsMapped Then

                                ctrl.XMLMapping.SetMapping("/ns0:root[1]/customer[1]/" + ctrl.Title.ToString.ToLower(),
                                            "xmlns:ns0=""urn:microsoft:ordermanager:customer""", customXMLPart)
                            End If
                        Next

                        word.Visible = True
                    End Using
                Catch ex As Exception
                    Throw New InvalidOperationException("Failed to create customer report.", ex)
                End Try
            End If
        End Sub

        Private Function CopyStreamToTempFile(ByVal stream As System.IO.Stream, ByVal ext As String) As String
            Dim path = GetTempFileName(ext)
            'Create the temp file
            Dim file = System.IO.File.Create(path)
            file.Close()
            'Write the stream to disk
            Using fileStream = System.IO.File.Open(path,
                                                   System.IO.FileMode.OpenOrCreate,
                                                   System.IO.FileAccess.Write,
                                                   System.IO.FileShare.None)

                Dim buffer(0 To stream.Length - 1) As Byte
                stream.Read(buffer, 0, stream.Length)
                fileStream.Write(buffer, 0, buffer.Length)

                fileStream.Close()
            End Using
            Return path
        End Function

        Private Function GetTempFileName(ByVal ext As String) As String
            'Return a uinuqe file name in My Documents\Reports based on a guid
            Dim path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Reports"
            If Not Directory.Exists(path) Then
                Directory.CreateDirectory(path)
            End If

            Dim filename = Guid.NewGuid().ToString() & ext
            path = System.IO.Path.Combine(path, filename)

            Return path
        End Function
    End Module

End Namespace






all this class i did not convert it properly and no online converter did convert properly too,
specially this part :

Dim myXML = <customer>
                                   <%= From prop In cust.Details.Properties.All
                                       Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
                                   %>
                               </customer>



any help for this???

解决方案

No need to ask such questions every time you need to translate between those languages. You can always do it automatically.
Please see my past answers:
COde Line Interpretatio of C# to VB.NET[^],
Need to convert vb code to c#[^],
FixedPage to ContentPage convert c# code into vb.net[^],
Source Code from a exe[^].

Pay special attention for most robust off-line method using open-source ILSpy. It provides great quality of translation.

—SA


Here you go:

Click me![^]


vb to c# and c# to vb


这篇关于从vb转换为c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆