当前位置:编程学习 > wap >>

wp8 异步调取数据怎么从委托中返回数据

wp8 异步调取数据怎么从委托中返回数据
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.IO

Public Class MainViewModel
    Implements INotifyPropertyChanged

    Dim uri As Uri

    Public Sub New()
        Me.Items = New ObservableCollection(Of ItemViewModel)()
        AddHandler Play, AddressOf cl
    End Sub

    ''' <summary>
    ''' ItemViewModel 对象的集合。
    ''' </summary>
    Public Property Items() As ObservableCollection(Of ItemViewModel)

    Private _sampleProperty As String = "Sample Runtime Property Value"
    ''' <summary>
    ''' 示例 ViewModel 属性;此属性在视图中用于使用绑定显示它的值
    ''' </summary>
    ''' <returns></returns>
    Public Property SampleProperty() As String
        Get
            Return _sampleProperty
        End Get
        Set(ByVal value As String)
            If Not value.Equals(_sampleProperty) Then
                _sampleProperty = value
                NotifyPropertyChanged("SampleProperty")
            End If
        End Set
    End Property

    ''' <summary>
    ''' 返回本地化字符串的示例属性
    ''' </summary>
    Public ReadOnly Property LocalizedSampleProperty() As String
        Get
            Return AppResources.SampleProperty
        End Get
    End Property

    Public Property IsDataLoaded() As Boolean

    ''' <summary>
    ''' 创建一些 ItemViewModel 对象并将其添加到 Items 集合中。
    ''' </summary>
    ''' 
    Dim debug As System.Diagnostics.Debug
    Dim resultString As String
    Public Delegate Sub PlayHandler(ByVal e As String)
    Public Event Play As PlayHandler
    Private Sub request_Callback(asyncResult As IAsyncResult)
        Try
            Dim webRequest As HttpWebRequest = asyncResult.AsyncState
            Dim webResponse As HttpWebResponse = webRequest.EndGetResponse(asyncResult)
            Dim streamResult As Stream = webResponse.GetResponseStream()
            Dim reader = New StreamReader(streamResult)
            resultString = reader.ReadToEnd()
            ' If OnGetInfoCompleted(this, resultString) <> DBNull.Value Then
            'fh1(resultString)
            'RaiseEvent OnGetInfoCompleted(this, resultString)
            ' Me.Items.Add(New ItemViewModel() With {.LineOne = resultString, .LineTwo = "Maecenas praesent accumsan bibendum", .LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu"})
            'End If
            If resultString <> "" Then
                RaiseEvent Play(resultString)
            End If
        Catch ex As WebException
            MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK)
        End Try

    End Sub

    Public Sub fh(ByVal url As String)
        Dim request As HttpWebRequest = HttpWebRequest.CreateHttp(url)
        request.Method = "GET"
        Dim resultString As String = String.Empty
        request.BeginGetResponse(New AsyncCallback(AddressOf request_Callback), request)
    End Sub
    Public Sub cl(e As String)

        'Me.Items.Add(New ItemViewModel() With {.LineOne = e, .LineTwo = e, .LineThree = e})
        Diagnostics.Debug.WriteLine(e)
        MessageBox.Show(e, "提示", MessageBoxButton.OK)
        'Me.Items.Add(New ItemViewModel() With {.LineOne = e})
        'Me.IsDataLoaded = True
    End Sub
    Public list As List(Of String)
    Public Sub LoadData()

        Dim uri As String = "http://www.baidu.com"
        fh(uri)
      
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private Sub NotifyPropertyChanged(ByVal propertyName As String)
        Dim handler As PropertyChangedEventHandler = PropertyChangedEvent
        If Nothing IsNot handler Then
            handler(Me, New PropertyChangedEventArgs(propertyName))
        End If
    End Sub
End Class

运行结果出现下面错误

请各位大神指教~ --------------------编程问答-------------------- 我看不懂VB的代码。。 --------------------编程问答-------------------- 题目和你给的图不对啊,另外你这不是WP8的代码吗。

wp8里面就没有同步的httpwebrequest

描述清楚啊楼主 --------------------编程问答--------------------
引用 2 楼 NianHui 的回复:
题目和你给的图不对啊,另外你这不是WP8的代码吗。

wp8里面就没有同步的httpwebrequest

描述清楚啊楼主

额,,参走眼了,,,

报错哪行放到begininvoke,跨线程访问的原因

补充:移动开发 ,  Windows Phone
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,