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