【发布时间】:2023-01-16 01:20:46
【问题描述】:
我已经从代码后面为依赖注入设置了 ViewModel。我想在 xaml 中保留 IntelliSense 建议的可能性。一切似乎都正常,但是一旦我添加了x:DataType="viewModels:HomeViewModel",我就收到了Number property not found 并且无法运行我的解决方案的错误。为什么会这样以及如何解决这个问题?
如果我删除x:DataType="viewModels:HomeViewModel",一切正常。
主页.xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Pages.HomePage"
xmlns:viewModels="clr-namespace:MyApp.ViewModels"
x:DataType="viewModels:HomeViewModel"
Title=""
NavigationPage.HasNavigationBar="False">
<Frame>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0"></Label>
<ListView ItemsSource="{Binding TotalData}" Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5">
<Label Text="{Binding Number}" Margin="0,0,10,0"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Frame>
</ContentPage>
【问题讨论】: