VB Magic

2012/11/08

OData AddTo using WinRT and VB.net

I was trying to find a way to POST a new entity to an OData feed and found it hard to find examples of doing this. After a bit of searching I found some C# code that allowed me to do an update. After converting this to VB I ended up with

    Private Sub addPersonButton_Click_1(sender As Object, e As RoutedEventArgs)

        Dim p As New Person
        p.name = personNameTextBox.Text
        If p.name <> "" Then
            _context.AddToPerson(p)
            _context.BeginSaveChanges(AddressOf ContextSaveChanges, p)
        End If
    End Sub

Person is part of an OData feed I am using e.g. http://server/odata.svc/Person

So I create a new instance of a Person and add a name to it (From a TextBox in the UI)

Then I use the AddToPerson method of the context and as there is no synchronous way in WinRT to SaveChanges, I used the BeginSaveChanges method of the context and passed through the address of the routine that will handle the callback.

    Private Function ContextSaveChanges(result As IAsyncResult) As Task
        Try
            _context.EndSaveChanges(result)
        Catch ex As Exception
            _message = ex.Message
            If ex.InnerException IsNot Nothing Then
                _message = _message & " - " & ex.InnerException.Message
            End If
            Me.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, AddressOf UpdateUI)
        End Try
    End Function

    Public Sub UpdateUI()
        personNameTextBox.Text = _message
    End

Above is the function that gets called and it will try to run EndSaveChanges and trap any error that occurred and display it on the UI. I originally put the personNameTextBox.Text = ex.Message in the catch section but that caused a Treading error [RPC_E_WRONG_THREAD]. So after a lot of routing around and the help of a colleague I discovered the Me.Dispacher.RunAsync function in which I had a lambda expression in to update the UI. This didn’t fail but also didn’t update the UI so I used a subroutine and put the AddressOf in the RunAsync command and this worked as expected.

Phew made it ūüėČ

2012/09/18

Windows Store App (Was Metro) And Shared Files

Filed under: Windows 8, Windows Store App — Tags: , , , — vbmagic @ 6:08 pm

Just a quick post about an issue I came across when trying to develop and app and share the files between my Laptop and Desktop via Dropbox.

I use this method quite successfully for most of my development work, but it seems different for Windows 8 Metro App Store development.

Problem

I build a windows store app on my Desktop and store the resultant solution in my dropbox directory structure. It builds and runs fine. I go to the office and load the application via my laptop. So far it all seems ok. It builds with no issues. But when you run it, you get an Application Failed to start error message. And looking into the exception, it boils down to a COM error:

“Error HRESULT E_FAIL has been returned from a call to a COM component.” err -2147467259

Next I created a similar app on the laptop it built and ran okay and then I loaded the app on my desktop and exactly the same thing happened.

Solution

Hopefully I’ll find out Friday at the Windows 8 DevCamp in London (21st September).

Blog at WordPress.com.