目前分類:銀光Silverlight4 (6)

瀏覽方式: 標題列表 簡短摘要
silverlight4 調用 WCF 的模式都是屬於非同步

如果要想取回WCF非同步回傳值 在silverlight專案裡必須使用事件完成模式來取回
這跟 多執行緒 BCAKGROUNFWORKER 的用法是一樣的 感覺

在這裡已經隱含了跨執行緒介面傳遞



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
'------模式判別----------------------
Dim srv = New ServiceReference1.Service1Client 'WCF類別
Dim mode As String = "0"




'---------對非同步的Completed事件 掛上委派動作-------------------------
srv.jk_getisTimeModeAsync(Label_ID.Content)
AddHandler srv.jk_getisTimeModeCompleted, Sub(sd, parameter)
mode = parameter.result
' MessageBox.Show("isTimemode=" + mode)
If mode = "1" Then
' MessageBox.Show("isTimemode=" + mode + ",啟動計時")
Label_isTimemode.Content = "isTimemode=" + mode + ",啟動計時模式"
timer_RTime.Interval = TimeSpan.FromMilliseconds(30 * 1000) '15秒檢查一次是否為預設時間
timer_RTime.Start()
Else
' MessageBox.Show("isTimemode=" + mode + ",不啟動計時")
Label_isTimemode.Content = "isTimemode=" + mode + ",不啟動計時模式"
timer_RTime.Stop()
End If


'------/模式判別/----------------------
End Sub
'---------/對非同步的Completed事件 掛上委派動作/-------------------------


''---------對非同步的Completed事件 掛上委派動作-------------------------
' srv.jkget_sessionIDAsync()
'AddHandler srv.jkget_sessionbyNameCompleted, Sub(sd, parameter)
' mode = parameter.Result
' MessageBox.Show(mode + "=mode")

' End Sub
''---------/對非同步的Completed事件 掛上委派動作/-------------------------



End Sub


End Class

prague12 發表在 痞客邦 留言(0) 人氣()

在silverlight專案檔下 引用WCF類別

需要使用非同步的機制來取得WCF回應的結果 非同步寫法需要委派掛上事件的動作 JK:"委派"相當於是掛上記憶體涵數指標

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim srv = New ServiceReference1.Service1Client 'WCF類別
Dim sID As String = "NN"

'---------對非同步的Completed事件 掛上委派動作-------------------------
AddHandler srv.jkget_sessionIDCompleted, Sub(sd, parameter)
sID = parameter.result
Label_ID.Content = sID
End Sub
'---------/對非同步的Completed事件 掛上委派動作/-------------------------

srv.jkget_sessionIDAsync()

End Sub

prague12 發表在 痞客邦 留言(0) 人氣()

Silverlight 上的多執行序之一 : DispatcherTimer

VB用法類似 winForm 的 Timer ,
全域使用 要先用事件引發定義 再handle給添加執行動作事件函式

jk 測試ok
'-------------------------------------
Private WithEvents timer1 As System.Windows.Threading.DispatcherTimer = New System.Windows.Threading.DispatcherTimer()

Private Sub TimerAction(ByVal sender As System.Object, ByVal e As EventArgs) Handles timer1.Tick

Static aa As Int16

If aa = 60 Then '記錄N次
timer1.Stop()
aa = 0
button3.Content = "Stop ticking"
End If
captureSourec0.CaptureImageAsync() '啟動非同步截圖
' MessageBox.Show("take snapshot " + aa.ToString)
' MessageBox.Show("AA=" + aa.ToString)
aa = aa + 1
End Sub

prague12 發表在 痞客邦 留言(0) 人氣()

Imports System.IO
Imports System.Drawing

Imports SilverlightApplication1.Web.ClassStream.ClassStream

Public Class fileupload
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Using sr = New StreamReader(Request.InputStream)

'--------寫出檔案------------------
'法1:
'Dim data = sr.ReadToEnd()
'Dim byteArrayD = Convert.FromBase64String(data)

'Dim byteArray_stream = New MemoryStream(byteArrayD) '把收到的bytes 轉為stream
'Dim pp As New Bitmap(byteArray_stream)
'pp.Save("c:/cc/Temppic_D.jpg") '檔名是已知的jpg 所以才能這樣定

'---------------------
'法2:
Dim streamLength = sr.BaseStream.Length
Dim byteArrayF = ConvertStreamToBytes(sr.BaseStream)

Dim fileName = Request.QueryString("fileName").ToString() '使用之前POST過來的參數fileName=tempPic.jpg
Using fs = New FileStream("C://cc/" + fileName, FileMode.Create)
fs.Write(byteArrayF, 0, byteArrayF.Length - 1) '從byteArray 寫入 fs stream
End Using
'----------/寫出檔案/----------------
MsgBox("Server 收到完成 創建路徑 C:/cc/")
End Using
End Sub

End Class

prague12 發表在 痞客邦 留言(0) 人氣()

Imports System.IO
Imports System.Net
Imports System.IO.IsolatedStorage

'用戶獨立空間 IsolatedStorage 基本上相當於cookie 可以自定容量大小

'--------建立iso空間-------------------------
'http://www.dotblogs.com.tw/larrynung/archive/2009/04/22/8128.aspx
'http://archive.cnblogs.com/a/1224186/

Using store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
' MessageBox.Show(store.Quota.ToString) '要增加空間配額前 不可以先檢查store.Quota 不然增加空間會失敗
' MessageBox.Show(store.AvailableFreeSpace.ToString) '可以檢查剩餘量
'Try
' Dim yesno = store.IncreaseQuotaTo(1048576 * 20) 'Then '增加空間配額到20MB
'MessageBox.Show(yesno.ToString)
' MessageBox.Show(store.Quota.ToString)
' MessageBox.Show(store.AvailableFreeSpace.ToString + "AvailableFreeSpace")



' Dim pic_isostreamW As IsolatedStorageFileStream = store.CreateFile(upfileName)
' Using sw As New StreamWriter(pic_isostreamW)
' sw.WriteLine("AABBCC")
'Dim pic_char(50) As Char
'For i = 0 To 50
' pic_char(i) = CChar(ChrW(pic_Bytes(i)))
'Next
'sw.Write(pic_char, 0, 50)

' sw.Flush()
' sw.Close()
'End Using

'Catch ex As Exception
' MessageBox.Show(ex.Message)
'End Try

'Dim files = store.GetFileNames '取得iso空間內所有檔案名稱
'Dim files_all_str As String = ""
'For i = 0 To files.Length - 1
' files_all_str = files_all_str + files(i) + vbNewLine
'Next
' MessageBox.Show(files_all_str)

' Dim directoriesInTheRoot = store.GetDirectoryNames ' 取得iso空間內所有資料夾名稱

'Dim pic_isostreamR As IsolatedStorageFileStream = store.OpenFile(upfileName, FileMode.Open, FileAccess.Read)
'Dim sr As New StreamReader(pic_isostreamR)
'Dim str = sr.ReadToEnd
'MessageBox.Show("sr read=" + str)

prague12 發表在 痞客邦 留言(0) 人氣()

參考 :
http://www.dotblogs.com.tw/puma/archive/2008/12/07/6289.aspx
http://www.cnblogs.com/anjou/archive/2007/12/07/986887.html

silverlight 版 webclient 資料上傳函式 用法 ' jk測試ok
'---資料上傳---------------
'法1:
'Dim array1 = ConvertStreamToBytes(pic_streamW)
'Dim data1 = Convert.ToBase64String(array1) '要先轉換成字串模式
'UploadData(upfileName, data1)
'---------------------------------
'法2:
UploadFile("tempPic_F.jpg", pic_streamW)
'----/資料上傳/----------------




'以下打包成一個Class類別庫 測試ok
'================================
Imports System.IO
Imports System.Net
Namespace ClassStream


Public Class ClassStream
Public Shared Function ConvertStreamToBytes(ByVal stream As Stream) As Byte()

Dim bytes() = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' // 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin)
Return bytes
End Function

Public Shared Sub StreamToFile(ByVal stream As Stream, ByVal fileName As String)

' // 把 Stream 转换成 byte[]
Dim bytes = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' // 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin)

' // 把 byte[] 写入文件
Dim fs = New FileStream(fileName, FileMode.Create)
Dim bw = New BinaryWriter(fs)
bw.Write(bytes)
bw.Close()
fs.Close()
End Sub

Public Shared Function FileToStream(ByVal fileName As String) As Stream

' // 打开文件
Dim fileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
' // 读取文件的 byte[]
Dim bytes() = New Byte(fileStream.Length) {}
fileStream.Read(bytes, 0, bytes.Length)
fileStream.Close()
' // 把 byte[] 转换成 Stream
' Dim stream = New MemoryStream(bytes)
Dim stream = BytesToStream(bytes)
Return stream
End Function


Public Shared Function BytesToStream(ByVal bytes As Byte()) As Stream

Dim stream = New MemoryStream(bytes)
Return stream
End Function



'//上傳檔案
Public Shared Sub UploadFile(ByVal savefileName As String, ByVal data As Stream)
Dim xapUri = Application.Current.Host.Source
Dim webUri = New Uri(xapUri, "../")
' MessageBox.Show(webUri.ToString())
Dim rootPath = webUri.ToString '取得當前目錄PATH
Dim upURL = rootPath + "fileupload.aspx" '注意: 一定要針對當前那個網頁才能POST成功


Dim ub = New UriBuilder(upURL)
ub.Query = String.Format("fileName={0}", savefileName)
Dim c = New WebClient()


'--------------事件委派動作---------------------------------------
'c.OpenWriteCompleted += (sender, e) =>
' {
' PushData(data, e.Result);
' e.Result.Close();
' data.Close();
' MessageBox.Show("上傳完畢");
' };

' vb對應的寫法如下
AddHandler c.OpenWriteCompleted, Sub(sender, e)
PushData(data, e.Result)
e.Result.Close()
data.Close()

End Sub

'------------/事件委派動作/---------------------------------------

c.OpenWriteAsync(ub.Uri, "POST")



End Sub

Public Shared Sub UploadData(ByVal savefileName As String, ByVal data_str As String)
Dim xapUri = Application.Current.Host.Source
Dim webUri = New Uri(xapUri, "../")
' MessageBox.Show(webUri.ToString())
Dim rootPath = webUri.ToString '取得當前目錄PATH
Dim upURL = rootPath + "fileupload.aspx" '注意: 一定要針對當前那個網頁才能POST成功


Dim ub = New UriBuilder(upURL)
ub.Query = String.Format("fileName={0}", savefileName)
Dim wc = New WebClient()

wc.UploadStringAsync(ub.Uri, data_str)


End Sub

Public Shared Sub PushData(ByVal input As MemoryStream, ByVal output As Stream)

Dim buffer() As Byte = New Byte(4000000) {} ' 4000K =4MB 暫存區

Dim stream As Stream = input
Dim read As Integer = 0
Dim total As Integer = 0


Do '回傳這次讀取了多少個 最多4096 個byte
read = stream.Read(buffer, total, 4096)


output.Write(buffer, total, read)
If (read <> 0) Then
total += read '累計總量
End If

Loop While (read <> 0)

'Dim bytesRead As Integer
'bytesRead = input.Read(buffer, 0, buffer.Length)
'While (input.Read(buffer, 0, buffer.Length)) <> 0

' output.Write(buffer, 0, bytesRead)

'End While

End Sub


End Class
End Namespace

prague12 發表在 痞客邦 留言(0) 人氣()