1

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

enter image description here

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?

  • Not enough info to tell. Delete all bin/obj folders in the solution. Restart visual studio. Rebuild all. If that doesn't fix it, delete the StudentView and re-create it. Don't copy over the generated root elements in XAML or the generated namespace and class name in the codebehind. –  May 22 '17 at 19:00
  • I have try to delete all in bin/obj/ excluded folder and try it to code from beginning, but I have the same error. – tokobersama May 23 '17 at 09:55
  • Hope the answer in this link might shed some light. https://stackoverflow.com/questions/85091/error-bc30002-type-xxx-is-not-defined – MICHAEL PRABHU May 23 '17 at 10:53

1 Answers1

0

Bit late to the party but maybe someone else struggles too.

The problem is probably the namespace, ns definitions work a little different in vb compared to c#

As you can see in your StudentView.xaml you are importing

Imports System.Collections.ObjectModel 
Imports MVVMDemo.MVVMDemo.Model
 
Namespace MVVMDemo.ViewModel

Note that MVVMDemo appears twice, while the namespace definition in your model only contains it once:

Namespace MVVMDemo.Model

The first originates from your project name, the second originates from your explicit namespace definition.

Since you didn't post your StudentView.xaml.vb i can only assume it contains

Namespace MVVMDemo.Views

This would require your StudentView.xaml to start like this:

<UserControl x:Class="MVVMDemo.MVVMDemo.Views.StudentView"

The reason is that vb adds the projectname in front of your defined namespace by default. So you can also fix your project by removing MVVMDemo from your namespace definitions.