英文:
"Cannot resolve type "my_type"". Data type error. C# MAUI
问题
I have TeamsPage.xaml, TeamsPage.xaml.cs, and TeamsPageViewModel.cs code, and you encountered an error related to the x:DataType attribute. You mentioned that you resolved it by removing x:DataType=(My viewmodel) and x:DataType=(Player). If you're looking for a more elegant solution, you can consider using xmlns aliases in your XAML to avoid the x:DataType attribute. Here's an example:
In your TeamsPage.xaml, you can define xmlns aliases like this:
xmlns:local="clr-namespace:_153502_Tolstoi.UI.ViewModels"
xmlns:models="clr-namespace:_153502_Tolstoi.Domain.Entities"
Then, in your DataTemplate and other XAML elements, you can use these aliases instead of x:DataType:
<DataTemplate local:TeamsPageViewModel.ItemType="{x:Type models:Player}">
<!-- Your DataTemplate content here -->
</DataTemplate>
This way, you avoid specifying x:DataType directly and use aliases, which can make your XAML code more elegant and maintainable.
英文:
I have TeamsPage xaml page and its viewmodel TeamsPageViewmodel. This Viewmodel has observable collection Players. And I want to show it on my page with collection view, but I have an error when i want to display name in cell in CollectionView with datatemplate.
TeamsPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="_153502_Tolstoi.UI.Pages.TeamsPage"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:viewModels="using:_153502_Tolstoi.UI.ViewModels"
xmlns:models="using:_153502_Tolstoi.Domain.Entities"
x:DataType="viewModels:TeamsPageViewModel"
x:Name="Page"
Title="TeamsPage">
<ContentPage.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Loaded"
Command="{Binding UpdateGroupListCommand }"/>
</ContentPage.Behaviors>
<Grid
Margin="20"
RowDefinitions="Auto, Auto"
ColumnDefinitions="Auto, Auto"
ColumnSpacing="20"
RowSpacing="20">
<Picker
Grid.Row="0"
HorizontalOptions="Center"
Grid.ColumnSpan="2"
TextColor="Black"
Title="Pick a team"
ItemsSource="{Binding Teams}"
SelectedItem="{Binding SelectedTeam}"
ItemDisplayBinding="{Binding Name}">
<Picker.Behaviors>
<toolkit:EventToCommandBehavior
EventName="SelectedIndexChanged"
Command="{Binding UpdateMembersListCommand}">
</toolkit:EventToCommandBehavior>
</Picker.Behaviors>
</Picker>
<CollectionView
Grid.ColumnSpan="2"
Grid.Row="1"
x:Name="CollectionView"
ItemsSource="{Binding Players}"
SelectionMode="None">
<CollectionView.EmptyView>
<Label
Text="No tours found!"
TextColor="LightGray"
FontSize="28"
HorizontalOptions="Center"
VerticalOptions="Center"
FontAttributes="Bold"/>
</CollectionView.EmptyView>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:Player">
<Border
BackgroundColor="{StaticResource Secondary}"
Margin="10,10,10,10"
HeightRequest="120"
Padding="20">
<Border.StrokeShape>
<RoundRectangle CornerRadius="10" ></RoundRectangle>
</Border.StrokeShape>
<HorizontalStackLayout>
<Label Text="{Binding Name}"
FontSize="18"
MaximumWidthRequest="120"
HorizontalTextAlignment="Center"
VerticalOptions="Start"
MaxLines="3"
Margin="0,20,0,0"
FontAttributes="Bold"
TextColor="Black"
HorizontalOptions="Center"/>
</HorizontalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<ActivityIndicator
IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
Grid.RowSpan="2"
Grid.ColumnSpan="2"
BackgroundColor="Transparent"
Color="{StaticResource Primary}"
HeightRequest="100"
WidthRequest="100"
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"/>
</Grid>
</ContentPage>
TeamsPage.xaml.cs
using _153502_Tolstoi.UI.ViewModels;
namespace _153502_Tolstoi.UI.Pages;
public partial class TeamsPage : ContentPage
{
private TeamsPageViewModel _teamsPageViewModel;
public TeamsPage(TeamsPageViewModel teamsPageViewModel)
{
InitializeComponent();
BindingContext = _teamsPageViewModel = teamsPageViewModel;
}
}
TeamsPageViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _153502_Tolstoi.Application.Abstractions;
using _153502_Tolstoi.Domain.Entities;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace _153502_Tolstoi.UI.ViewModels
{
public partial class TeamsPageViewModel : BaseViewModel
{
private readonly ITeamService _teamService;
private readonly IPlayerService _playerService;
public TeamsPageViewModel(ITeamService teamService, IPlayerService
playerService)
{
_teamService = teamService;
_playerService = playerService;
}
public ObservableCollection<Team> Teams { get; set; } = new();
public ObservableCollection<Player> Players { get; set; } = new();
[ObservableProperty]
Team selectedTeam;
[RelayCommand]
async void UpdateGroupList() => await GetTeams();
[RelayCommand]
async void UpdateMembersList() => await GetPlayers();
public async Task GetTeams()
{
IsBusy = true;
var cources = await _teamService.GetAllAsync();
await MainThread.InvokeOnMainThreadAsync(() =>
{
Teams.Clear();
foreach (var cource in cources)
Teams.Add(cource);
});
SelectedTeam = Teams.FirstOrDefault();
IsBusy = false;
}
public async Task GetPlayers()
{
IsBusy = true;
var players = (await _teamService.GetByIdAsync(SelectedTeam.Id)).Players;
await MainThread.InvokeOnMainThreadAsync(() =>
{
Players.Clear();
foreach (var player in players)
Players.Add(player);
});
IsBusy = false;
}
}
}
error:
error XFC0000 Cannot resolve type "using:_153502_Tolstoi.Domain.Entities:models:Player". 71
I have same code on my other project and it works normal, but models in this project were not in library. Maybe it is a reason of error, but how to solve it
Edited: i solved it by removing x:DataType=(My viewmodel) and x:DataType=(Player). But cant be some more elegant solutions?
答案1
得分: 0
如果类型定义在不同的程序集中,您必须指定程序集
xmlns:behaviors="clr-namespace:Behaviors;assembly=BehaviorsLibrary"
英文:
if the type is defined in a different assembly, you have to specify the assembly
xmlns:behaviors="clr-namespace:Behaviors;assembly=BehaviorsLibrary"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论