I am beginner in MVVM and WPF Iam Using VB.NET 2015 I try the tutorial in tutorialspoint and the code in C# while iam use vb.net, i use telerik to convert from c# to vb. I did everything as in this tutorial. But I'm still getting an exception:
BC3002: Type 'BillingBersama.BillingBersama.Views.StudentView' is not defined .. MainWindow.xaml 67
The line error, MainWindow.xaml:
<views:StudentView x:Name="StudentViewControl" Loaded="StudentViewControl_Loaded" />
here the code
StudentModel.vb
Imports System.ComponentModel
Namespace MVVMDemo.Model
Public Class StudentModel
End Class
Public Class Student
Implements INotifyPropertyChanged
Private m_firstName As String
Private m_lastName As String
Public Property FirstName() As String
Get
Return m_firstName
End Get
Set
If m_firstName <> Value Then
m_firstName = Value
RaisePropertyChanged("FirstName")
RaisePropertyChanged("FullName")
End If
End Set
End Property
Public Property LastName() As String
Get
Return m_lastName
End Get
Set
If m_lastName <> Value Then
m_lastName = Value
RaisePropertyChanged("LastName")
RaisePropertyChanged("FullName")
End If
End Set
End Property
Public ReadOnly Property FullName() As String
Get
Return Convert.ToString(m_firstName & Convert.ToString(" ")) & m_lastName
End Get
End Property
Public Event PropertyChanged As PropertyChangedEventHandler
Private Event INotifyPropertyChanged_PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub RaisePropertyChanged([property] As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs([property]))
End Sub
End Class
End Namespace
StudentViewModel.vb
Imports System.Collections.ObjectModel
Imports MVVMDemo.MVVMDemo.Model
Namespace MVVMDemo.ViewModel
Public Class StudentViewModel
Private Student As Object
Public Property Students() As ObservableCollection(Of Student)
Get
Return m_Students
End Get
Set
m_Students = Value
End Set
End Property
Private m_Students As ObservableCollection(Of Student)
Public Sub LoadStudents()
Dim students__1 As New ObservableCollection(Of Student)()
students__1.Add(New Student() With {
.FirstName = "Mark",
.LastName = "Allain"
})
students__1.Add(New Student() With {
.FirstName = "Allen",
.LastName = "Brown"
})
students__1.Add(New Student() With {
.FirstName = "Linda",
.LastName = "Hamerski"
})
Students = students__1
End Sub
End Class
End Namespace
StudentView.xaml
<UserControl x:Class="MVVMDemo.Views.StudentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local ="clr-namespace:MVVMDemo.MVVMDemo.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel HorizontalAlignment = "Left">
<ItemsControl ItemsSource = "{Binding Path = Students}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBox Text = "{Binding Path = FirstName, Mode = TwoWay}"
Width = "100" Margin = "3 5 3 5"/>
<TextBox Text = "{Binding Path = LastName, Mode = TwoWay}"
Width = "100" Margin = "0 5 3 5"/>
<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}"
Margin = "0 5 3 5"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</UserControl>
MainWindow.xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMDemo"
xmlns:views ="clr-namespace:MVVMDemo.MVVMDemo.Views"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!--<views:StudentView x:Name = "StudentViewControl" Loaded = "StudentViewControl_Loaded"/>-->
<!--this line is the error-->
<views:StudentView x:Name="StudentViewControl" Loaded="StudentViewControl_Loaded" />
</Grid>
</Window>
MainWindow.xaml.vb
Namespace MVVMDemo
Partial Public Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeComponent()
Throw New NotImplementedException()
End Sub
Private Sub StudentViewControl_Loaded(sender As Object, e As RoutedEventArgs)
Dim studentViewModelObject As New ViewModel.StudentViewModel()
studentViewModelObject.LoadStudents()
'this Line: "StudentViewControl" Is Error, copy from convert telerik
'then I fix it from recomended VS Intellisense "Dim StudentViewControl As Object = Nothing"
Dim StudentViewControl As Object = Nothing
StudentViewControl.DataContext = studentViewModelObject
End Sub
End Class
End Namespace
Application.xaml
<Application x:Class="MVVMDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMDemo"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
So that's it. If somebody would me explain what's going on here? Why am I getting this error?