I would like to create an application that is gong to connect to a web service and return some data. I have been following a few tutorials and have the following code:
Public Class ItemViewModel
Implements INotifyPropertyChanged
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
If Not value.Equals(_Name) Then
_Name = value
NotifyPropertyChanged("Name")
End If
End Set
End Property
..... along with other properties resembling the table from my database.
I then have a MainViewModel
Private _Customer As New CustomerService.Customer
Public Sub LoadData()
Dim myService As New CustomerService.CustomerClient
myService.GetCustomerByNameAsync("Andy")
AddHandler myService.GetCustomerByNameCompleted, AddressOf CustomerByName
Items.Add(NewItemViewModel With {.Name = _Customer.Name})
End Sub
Private Sub CustomerByName(sender As Object, e As CustomerService.GetCustomerByNameCompletedEventArgs)
_Customer = e.Result
End Sub
The problem i have is the service returns data when i check with WCF test tool but on this occasion when running the app i keep getting an empty object for _Customer. I have tried making it into a shared variable but nothing seems to hold the data i get from the service.
How i could hold the data in my MainViewModel?
Thanks
The problem is that GetCustomerByNameAsync execute asynchronously so Items.Add(NewItemViewModel With {.Name = _Customer.Name}) execute before the service respon and the code inside CustomerByName execute. If you move the Add code inside the event handler (CustomerByName) it should work like you want.
Related
I've created a request class. Here is an abbreviated version of it:
Public Class Request(Of T)
Private _Account As String
Public Property Account() As String
Get
Return _Account
End Get
Set(ByVal value As String)
_Account = value
End Set
End Property
Private _InnerRequest As T
Public Property InnerRequest() As T
Get
Return Me._InnerRequest
End Get
Set(ByVal value As T)
Me._InnerRequest = value
End Set
End Property
End Class
And then I have two other classes that I intend to use with this one - again, abbreviated
Public Class Individual
Public FirstName As String
Public LastName As String
Friend Sub New()
End Sub
End Class
And
Public Class Commercial
Public EntityName As String
Friend Sub New()
End Sub
End Class
Again, both of these are pretty abbreviated. The issue comes in when I attempt to use the properties of individual or commercial:
Dim Req As New Request(Of Individual)()
Req.InnerRequest.FirstName = "Herman" <-- Null Ref Exception
So... how do I get my inner request null ref exception kicked? I tried simply using Me._InnerRequest = New T in the New sub of Request, but no dice. Is there a way to handle this?
Req.InnerRequest must be set to an object instance of Individual first.
Req.InnerRequest = new Individual()
Req.InnerRequest.FirstName = "Herman"
Or create an instance for InnerRequest with the following modifications
Public Class Request(Of T As {New}) 'Classes of type T must have a public new constructor defined
::
Private _InnerRequest As New T() 'Creates a new class of type T when an instance is created of Request
And make the constructors of the other classes Public instead of Friend.
Than you can directly do
Dim Req As New Request(Of Individual)()
Req.InnerRequest.FirstName = "Herman"
#Barry already answered what the main problem is, but here's an alternate syntax if you prefer object initializers:
Req.InnerRequest = new Individual() With { FirstName = "Herman" }
Or, if you prefer, you could overload the constructor for your Individual class:
Dim individual As New Individual("Herman")
Req.InnerRequest = individual
With the Individual class looking like:
Public Class Individual
Public FirstName As String
Public LastName As String
Friend Sub New()
End Sub
Friend Sub New(firstName As String)
Me.FirstName = firstName
End Sub
End Class
You probably should consider restricting the T to some Entity class:
Public Class Request(Of T As Entity)
From which both Individual and Commercial will inherit:
Public Class Individual : Inherits Entity
Then maybe declare an overridable property Name of type String on this Entity class (which can be abstract/MustInherit), this should provide some flexibility. Otherwise you'd be having a hard time consuming your design pattern.
I am trying to seperate my code logic from my gui as in MVC principles, what I am trying to achieve is quite simple I believe
I have my Form1, which contains a textbox and button, once the button is clicked it loads a function in my controller class which adds a string to a database using entity and then should update the textbox with this name.
I thought what I would need to do is pass the original form through and then databind to the textbox object on the form, this is where I have come unstuck though, as my logic fails...
Public Class Form1
Private mf As New MainForm(Me)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mf.buttonClick()
End Sub
End Class
Public Class MainForm
Private Property a As Form
Public Sub New(ByVal s As Form)
a = s
End Sub
Function buttonClick() As Boolean
Dim context As TestDBEntities2 = New TestDBEntities2
Dim newCategory As tTable = New tTable
newCategory.Name = "Test1 " & Today.DayOfWeek
context.tTables.Add(newCategory)
context.SaveChanges()
Dim current As String = newCategory.Name
a.DataBindings.Add("text", "TextBox1", current)
Return True
End Function
End Class
and my error:
Cannot bind to the property or column Test1 6 on the DataSource.
Am I looking at this the right way? Or am I so far off that there is an obvious reason this doesn't work?
Any input would be appreciated! Whats the best way to pass data back to a source without returning it in as a result of a function?
You should consider changing your code a bit, so that it reflects more the MVC structure:
use Events to exchange data and indicate action triggers, instead of using the form object
normally the controller has knowledge of the form and not the other way around, so swap this in your project. This reflects also the first point
So a possible solution for a Windows Forms application could look like this:
The form that has one button and one text field, one event to signal the button click and one WriteOnly property to fill the TextBox from outside the form:
Public Class MainForm
Public Event GenerateAndShowEvent()
' allow text box filling
Public WriteOnly Property SetTextBoxContent()
Set(ByVal value)
generatedInputTextBox.Text = value
End Set
End Property
Private Sub generateAndShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateAndShowButton.Click
' forward message: inform the subscriber that something happened
RaiseEvent GenerateAndShowEvent()
End Sub
End Class
The controller class has knowledge of the form, creates it, binds (listens) to the form's button click event and makes the form ReadOnly for the world (exposes the form):
Public Class MainFormController
' the form that the controller manages
Private dialog As MainForm = Nothing
Public Sub New()
dialog = New MainForm()
' bind to the form button click event in order to generate the text and response
AddHandler dialog.GenerateAndShowEvent, AddressOf Me.GenerateAndShowText
End Sub
' allow the world to access readonly the form - used to start the application
Public ReadOnly Property GetMainForm()
Get
Return dialog
End Get
End Property
Private Sub GenerateAndShowText()
' create the text
Dim text As String = "Test test test"
' access the Database ...
' give the generated text to the UI = MainForm dialog!
dialog.SetTextBoxContent = text
End Sub
End Class
Now what left is to create first the controller, that creates the form and use the form to show it. This can be done like this:
Create an AppStarter module with a Main method:
Module AppStarter
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
' create the controller object
Dim controller As MainFormController = New MainFormController()
' use the public property to get the dialog
Application.Run(controller.GetMainForm)
End Sub
End Module
In your project settings uncheck the enable application framework setting and set the AppStarter module as the start point of your project:
Now you have a Windows Form Project using the MVC pattern.
If you still want to use DataBinding for the TextBox control, then create a Data Transfer Object or DTO that represents the fields you will transfer from your controller to the form:
Public Class DataContainer
Private t As String
Private i As Integer
Public Property Text() As String
Get
Return t
End Get
Set(ByVal value As String)
t = value
End Set
End Property
Public Property Id() As Integer
Get
Return i
End Get
Set(ByVal value As Integer)
i = value
End Set
End Property
End Class
Then add a BindingSource for your TextBox and configure it to use the DTObject:
Now bind the TextBox control to the DataBinding control:
What left is to add a public setter for the TextBox data binding control in the form:
Public Property TextBoxDataSource()
Get
Return TextBoxBindingSource.DataSource
End Get
Set(ByVal value)
TextBoxBindingSource.DataSource = value
End Set
End Property
and transfer the data from the controller:
Private Sub GenerateAndShowText()
' create the text
Dim text As String = "Test test test"
' access the Database ...
' give the generated text to the UI = MainForm dialog!
'dialog.SetTextBoxContent = text
Dim data As DataContainer = New DataContainer
data.Text = text
data.Id = 1 ' not used currently
dialog.TextBoxDataSource = data
End Sub
The binding can also be set programmatically - instead of doing this over the control property window, add the following code in the constructor of the form:
Public Sub New()
InitializeComponent()
' bind the TextBox control manually to the binding source
' first Text is the TextBox.Text property
' last Text is the DataContainer.Text property
generatedInputTextBox.DataBindings.Add(New Binding("Text", TextBoxBindingSource, "Text"))
End Sub
You seem to misunderstand the Add method.
The first argument is the name of the control's property to which you are binding. This should be "Text", not "text".
The second argument is an object that contains the data you want to bind. You have passed the name of the target control, rather than the source of the data. You are also binding to the form rather than the text box. So what you have said is that you want to bind the form's text property to data that can be extracted from the string "TextBox1".
The third argument says where to go to find the data. For example, if you passed a FileInfo object for the second argument, and you wanted to bind the file's path, you would pass the string "FullName", because that is the name of the property containing the data you want. So you have told the binding to look for a property on the string class called "Test1 6", which is why you have received the error message saying it can't be found.
I think what you want is
a.TextBox1.DataBindings.Add("Text", newCategory, "Name");
I have a class that contains a list of data that gets populated from a datastream. I want to bind that list to a datagridview so it automatically populates as the list does. However the list isn't refreshing as the data comes in. I have something like this.
Public class MyClass
Private mData as list(Of networkData)
Public Property Data() as list(Of networkData)
Get
return mData
End Get
Set
mData = value
End Set
End Property
' some other properties that aren't imporant
' stuff to load Data with data from network stream
end class
Public class networkDat
Private rawdata as string
Public Property rawdata() as string
Get
return mrawdata
End Get
Set
mrawData = value
End Set
End Property
' some other properties that aren't imporant
' functions to parse rawdata into the other properties
End Class
'form
Public Class dataviewer
Dim dataView as datagridViewer = new datagridviewer()
Private Sub dataviewer_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dim x as myClass = new myClass() 'new will start the datastream
datagridview.datasource = x.Data
End Sub
Since I started the datastream first dataviewer will have an inital set of data. However it doesn't update as new data comes in.
The List class does not implement the IBindingList interface which supports list change notifications.
Try using the BindingList class instead:
Provides a generic collection that supports data binding.
To observe changes to the properties in your class, the class would need to implement the INotifyPropertyChanged interface:
Notifies clients that a property value has changed.
List(of ) does not support change notifications. For your purpose, change the List(of ) to a ObservableCollection(of ).
In VB.NET, I am trying to retrieve what services are running on a TS using the following code:
Imports System.ServiceProcess
...
Dim dictservice As Generic.Dictionary(Of String, Services)
Public Sub GetService()
Dim localServices As ServiceController() = ServiceController.GetServices()
For Each service As ServiceController In localServices
dictservice.Add(service.DisplayName, New Services(service.DisplayName, service.ServiceName, service.Status.ToString))
Next
End Sub
My services class is as follows:
Class Services
Private _displayName As String
Private _serviceName As String
Private _serviceStatus As String
Sub New(ByVal DisplayName As String, ByVal ServiceName As Object, ByVal ServiceStatus As String)
_displayName = DisplayName
_serviceName = ServiceName
_serviceStatus = ServiceStatus
End Sub
Public Overrides Function ToString() As String
Return _serviceStatus
End Function
End Class
When i step through in debug mode it seems to being to populate the dictionary
display name: Application Experience
service name: AElookUpSVC
service status: Running (4)
When it tries to move onto the next item i get the following error:
Null reference exception was unhandled:
Object reference not set to an instance of an object.
I can't for the life of me work out where it is finding a null reference?
You need to initialize your dictionary with New:
Dim dictservice As New Generic.Dictionary(Of String, Services)
Public Sub GetService()
Dim localServices As ServiceController() = ServiceController.GetServices()
For Each service As ServiceController In localServices
dictservice.Add(service.DisplayName, New Services(service.DisplayName, service.ServiceName, service.Status.ToString))
Next
End Sub
Right now it's Nothing, hence the NullReferenceException.
The most likely problem appears to be that dictService is Nothing and hence you get a NullReferenceException. Where do you initialize / declare dictService and are you certain the initialization happens before this method?
If this is not the problem have you tried running this with the debugger attached? it should point you to the line that is failing.
I am looking for an efficient way to create a dynamic CrudViewModelBase<TConext, TEntity> that will be used as a protortype for all the ViewModels in the application, that are going to perform CRUD operations.
I don't know where is the efficient way to instantiate the DomainContext, should be application-level? ViewModel-level? please share me with your experience.
I am pretty new to MVVM, and I want to create a reusable ViewModelBase to perform these operation.
Any links, code-samples, or recommendations will be really welcommed.
I start writing some stuff (I am new to RIA as well), I will be out for few hours sorry for delay in comments, and thanks for cooperating:
Imports System.ServiceModel.DomainServices.Client
Imports Microsoft.Practices.Prism.ViewModel
Imports Microsoft.Practices.Prism.Commands
Imports CompleteKitchens.Model
Namespace ViewModel
Public MustInherit Class CrudViewModel(Of TContext As DomainContext, TEntity As Entity)
Inherits notificationobject
Protected Sub New(context As DomainContext, query As EntityQuery(Of TEntity))
m_Context = context
m_Query = query
End Sub
Private ReadOnly m_Context As TContext
Protected ReadOnly Property Context() As TContext
Get
Return m_Context
End Get
End Property
Private ReadOnly m_Query As EntityQuery(Of TEntity)
Protected ReadOnly Property Query As EntityQuery(Of TEntity)
Get
Return m_Query
End Get
End Property
Private m_IsLoading As Boolean
Public Overridable Property IsLoading As Boolean
Get
Return m_IsLoading
End Get
Protected Set(value As Boolean)
m_IsLoading = value
RaisePropertyChanged(Function() IsLoading)
End Set
End Property
Private m_Items As IEnumerable(Of TEntity)
Public Property Items() As IEnumerable(Of TEntity)
Get
Return m_Items
End Get
Set(ByVal value As IEnumerable(Of TEntity))
m_Items = value
RaisePropertyChanged(Function() Items)
End Set
End Property
Private m_CanLoad As Func(Of Boolean)
Protected Overridable ReadOnly Property CanLoad As Func(Of Boolean)
Get
If m_CanLoad Is Nothing Then m_CanLoad = Function() True
Return m_CanLoad
End Get
End Property
Private m_LoadCommand As ICommand
Public ReadOnly Property LoadCommand() As ICommand
Get
If m_LoadCommand Is Nothing Then m_LoadCommand = New delegatecommand(AddressOf Load, CanLoad())
Return m_LoadCommand
End Get
End Property
Private Sub Load()
IsLoading = True
operation = Context.Load(Query, False)
End Sub
Private m_CanCancel As Func(Of Boolean) = Function() operation.CanCancel
Protected Overridable ReadOnly Property CanCancel As Func(Of Boolean)
Get
Return m_CanCancel
End Get
End Property
Private m_CancelCommand As ICommand
Public ReadOnly Property CancelCommand() As ICommand
Get
If m_CancelCommand Is Nothing Then m_CancelCommand = New DelegateCommand(AddressOf Cancel, CanCancel())
Return m_CancelCommand
End Get
End Property
Private Sub Cancel()
operation.Cancel()
End Sub
Private WithEvents operation As LoadOperation(Of TEntity)
Private Sub operation_Completed(sender As Object, e As EventArgs) Handles operation.Completed
If operation.IsComplete Then
Items = operation.AllEntities
ElseIf operation.IsCanceled Then
End If
End Sub
End Class
End Namespace
As giddy stated VM's and repositories are concerned with separate things. Just like with ASP.net MVC you wouldn't try and put that stuff in your controller. The ViewModel provides a model of the display and GUI things. Think of the ViewModel as the display. Take the view away and you should be able to test the functionality of the view by testing the ViewModel.
In my current project I'm making calls to my RIA domain services from the ViewModel and then mapping the results to my VM. If you want to use the repository pattern download the code from the silverlight firestarter event. http://channel9.msdn.com/Series/Silverlight-Firestarter/Silverlight-Firestarter-2010-Session-3-Building-Feature-Rich-Business-Apps-Today-with-RIA-Services session 3 Dan Wahlin, there is an example of this. The video is a good watch also.
This example by Shawn Wildermuth shows the client side model actually talks to RIA services. I haven't implemented it yet but it makes more sense to me as the model in his example feels more like a controller to me.
http://wildermuth.com/2010/04/16/Updated_RIA_Services_MVVM_Example
I personally don't like binding Data model entities directly to ViewModels. In my current project I don't have a choice because all access is done through procs so I map proc results to ViewModels. If I did have table access I would probably still map data model entities to ViewModels. I'm not sure that's "correct" in the mvvm pattern but it allows you to keep your data model entities clean of display/validation attributes.