'ExampleMvvmPhone.ViewModel'是'命名空间',但用作“类型”

时间:2014-01-08 11:13:22

标签: c# wpf windows mvvm

我一直在按照教程创建一个在C#/ Windows手机中工作的MVVM原型。这是我的主要。但我得到的错误是ExampleMvvmPhone.ViewModelnamespace但是像type一样使用。请在这里帮助我。谢谢

namespace ExampleMvvmPhone
{ 
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            ViewModel viewModel = new ViewModel();
            this.DataContext = viewModel;
        }
    }
}

我的xaml

<TextBox x:Name="txtFirstName" Grid.Column="2"  Grid.Row="2" Margin="0,0,0,5" FontSize="24" Text="{Binding Current.FirstName, Mode=TwoWay}"/>
<TextBox x:Name="txtLastName" Grid.Column="2"  Grid.Row="3" Margin="0,0,0,5" FontSize="24" Text="{Binding Current.LastName, Mode=TwoWay}"/>
<TextBox x:Name="txtEmail" Grid.Column="2"  Grid.Row="4"  Margin="0,0,0,5" FontSize="24" Text="{Binding Current.Email, Mode=TwoWay}"/>
<TextBox x:Name="txtPhone" Grid.Column="2"  Grid.Row="5"  Margin="0,0,0,5" FontSize="24" Text="{Binding Current.Phone, Mode=TwoWay}"/>

我的ViewModel类:

    namespace ExampleMvvmPhone.ViewModel
    {
    public class ViewModel
    {
        private List<Customer> customers;
        private int currentCustomer;

        //constructor
        public ViewModel()
        {
            this.currentCustomer = 0;
            this.customers = new List<Customer>
            {
               new Customer
               {
                   CustomerID = 1,
                   Title = "Mr",
                   FirstName = "Mwangi",
                   LastName = "Austin",
                   Email = "mwangi@gmail.com",
                   Phone = "0728704660"
               },
               new Customer
               {
                   CustomerID = 2,
                   Title = "Mrs",
                   FirstName = "Christine",
                   LastName = "Nekunda",
                   Email = "mwangi@gmail.com",
                   Phone = "0728704660"
               },
               new Customer
               {
                   CustomerID = 3,
                   Title = "Ms",
                   FirstName = "Catherine",
                   LastName = "Nakutenga",
                   Email = "mwangi@gmail.com",
                   Phone = "0728704660"
               }
            };
        }

        public Customer Current
        {
            get
            {
                return this.customers[currentCustomer];
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

问题出在这一行:

ViewModel viewModel = new ViewModel();

由于您位于不同的命名空间中,您应该包含使用using定义的命名空间ViewModel:

using ExampleMvvmPhone.ViewModel;
namespace ExampleMvvmPhone
{
...

或使用将其添加到类名:

ViewModel.ViewModel viewModel = new ViewModel.ViewModel();

答案 1 :(得分:0)

似乎错误告诉您确切的问题是什么:

  

'ExampleMvvmPhone.ViewModel'是'命名空间',但用作“类型”

如果您查看代码,那么您将看到 将您的命名空间和类名命名为ViewModel。改变这个:

namespace ExampleMvvmPhone.ViewModel
{
    public class ViewModel
    {        ...

到此:

namespace ExampleMvvmPhone
{
    public class ViewModel
    {
        ...

答案 2 :(得分:0)

在您的ViewModel课程中,将您的名称空间namespace ExampleMvvmPhone.ViewModel更改为namespace ExampleMvvmPhone。因为如果对namespace ExampleMvvmPhone.ViewModel类使用ViewModel,那么编译器就会感到困惑,ViewModelclass还是namespace

由于