c# - 'ExampleMvvmPhone.ViewModel' 是 'namespace',但用作 'type'

标签 c# wpf windows mvvm

我一直在按照教程创建一个在 C#/Windows 手机中工作的 MVVM 原型(prototype)。这是我的主要内容。但我得到的错误是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];
            }
        }
    }
}

最佳答案

问题出在这一行:

ViewModel viewModel = new ViewModel();

由于您在不同的命名空间中,因此您应该包含在 using 中定义的命名空间 ViewModel。 :
using ExampleMvvmPhone.ViewModel;
namespace ExampleMvvmPhone
{
...

或使用将其添加到类名:
ViewModel.ViewModel viewModel = new ViewModel.ViewModel();

关于c# - 'ExampleMvvmPhone.ViewModel' 是 'namespace',但用作 'type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993980/

相关文章:

c# - 如何在 .NET Core 控制台应用程序中设置 DI 容器?

c# - 如何创建自定义 httpHandler 并将其添加到现有 IIS 网站

c# - ResourceDictionary : {"Key cannot be null.\r\nParameter name: key"} 中的运行时错误

c++ - Qt 与 WinRT C++ 构建问题

windows - Tortoise SVN "Can' t 读取连接错误”

c++ - windows编程如何让 "choose file"函数生效?

c# - Mkbundle Mono Assembly 绑定(bind)重定向

c# - 如何使用 NumericUpDown 计算文本框值的总数?

wpf - 绑定(bind)到 View 模型中的可见性是一个好的设计吗

C# WPF - 任务与多线程,以及从中更新 UI 组件