[英]Unable to map namespaces to xmlns namespaces in xaml
我正在關注有關MVVM的教程,並且遇到了一些問題。
我創建了4個文件夾,分別代表4個不同的名稱空間。 在MainPage.xaml
我使用以下代碼對這些名稱空間進行引用:
xmlns:viewModels="using:ViewModels"
xmlns:converters="using:Converters"
這些屬性在Page
屬性中。
接下來,我需要使用以下代碼來使用它們:
<Page.Resources>
<converters:ObjectExistsToVisible x:Key="ObjectExistsToVisible" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Vertical">
<ListView x:Name="MainList"
ItemsSource="{x:Bind Organization.People, Mode=OneWay}"
SelectedIndex="{x:Bind Organization.SelectedIndex, Mode=TwoWay}"
MinWidth="250" Margin="5">
<ListView.ItemTemplate>
<!-- The error is with x:DataType="" -->
<DataTemplate x:DataType="viewModels:PersonViewModel" >
<TextBlock Text="{x:Bind Name, Mode=OneWay}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Add" Click="{x:Bind Organization.Add}" Margin="5"/>
</StackPanel>
<StackPanel Grid.Column="2" Orientation="Vertical">
<TextBox
Text="{x:Bind Organization.SelectedPerson.Name, Mode=TwoWay, FallbackValue=''}"
Margin="5" />
<TextBox
Text="{x:Bind Organization.SelectedPerson.Age, Mode=TwoWay, FallbackValue='0'}"
Margin="5" />
<Button Content="Delete" Click="{x:Bind Organization.Delete}" Margin="5" />
</StackPanel>
</Grid>
可以在以下鏈接找到該教程:
問題是,出現以下錯誤:
名稱“ ObjectExistsToVisible”在名稱空間“ using:Converters”中不存在
名稱“ PersonViewModel”在名稱空間“ using:ViewModels”中不存在
我很確定它們確實存在。
按照要求:
PersonViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;
namespace ViewModels
{
public class PersonViewModel : NotificationBase<Person>
{
public PersonViewModel(Person person = null) : base(person) { }
public String Name
{
get { return This.Name; }
set { SetProperty(This.Name, value, () => This.Name = value); }
}
public int Age
{
get { return This.Age; }
set { SetProperty(This.Age, value, () => This.Age = value); }
}
}
}
經過Will的好評后,解決方案如下:
首先,通過Build > Clean Solution
。 接下來,通過Build > Rebuild Solution
。
這為我修復了錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.