"Cannot resolve type "my_type"". Data type error. C# MAUI

huangapple go评论76阅读模式
英文:

"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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;_153502_Tolstoi.UI.Pages.TeamsPage&quot;
             xmlns:toolkit=&quot;http://schemas.microsoft.com/dotnet/2022/maui/toolkit&quot;
             xmlns:viewModels=&quot;using:_153502_Tolstoi.UI.ViewModels&quot;
             xmlns:models=&quot;using:_153502_Tolstoi.Domain.Entities&quot;
             x:DataType=&quot;viewModels:TeamsPageViewModel&quot;
             x:Name=&quot;Page&quot;
             Title=&quot;TeamsPage&quot;&gt;

    &lt;ContentPage.Behaviors&gt;
        &lt;toolkit:EventToCommandBehavior 
            EventName=&quot;Loaded&quot;
            Command=&quot;{Binding UpdateGroupListCommand }&quot;/&gt;
    &lt;/ContentPage.Behaviors&gt;

    &lt;Grid
        Margin=&quot;20&quot;
        RowDefinitions=&quot;Auto, Auto&quot;
        ColumnDefinitions=&quot;Auto, Auto&quot;
        ColumnSpacing=&quot;20&quot;
        RowSpacing=&quot;20&quot;&gt;

        &lt;Picker
            Grid.Row=&quot;0&quot;
            HorizontalOptions=&quot;Center&quot;
            Grid.ColumnSpan=&quot;2&quot;
            TextColor=&quot;Black&quot;
            Title=&quot;Pick a team&quot;
            ItemsSource=&quot;{Binding Teams}&quot;
            SelectedItem=&quot;{Binding SelectedTeam}&quot;
            ItemDisplayBinding=&quot;{Binding Name}&quot;&gt;

            &lt;Picker.Behaviors&gt;
                &lt;toolkit:EventToCommandBehavior
                    EventName=&quot;SelectedIndexChanged&quot;
                    Command=&quot;{Binding UpdateMembersListCommand}&quot;&gt;
                &lt;/toolkit:EventToCommandBehavior&gt;
            &lt;/Picker.Behaviors&gt;

        &lt;/Picker&gt;

        &lt;CollectionView 
            Grid.ColumnSpan=&quot;2&quot;
            Grid.Row=&quot;1&quot;
            x:Name=&quot;CollectionView&quot;
            ItemsSource=&quot;{Binding Players}&quot;
            SelectionMode=&quot;None&quot;&gt;

            &lt;CollectionView.EmptyView&gt;
                &lt;Label
                    Text=&quot;No tours found!&quot;
                    TextColor=&quot;LightGray&quot;
                    FontSize=&quot;28&quot;
                    HorizontalOptions=&quot;Center&quot;
                    VerticalOptions=&quot;Center&quot;
                    FontAttributes=&quot;Bold&quot;/&gt;
            &lt;/CollectionView.EmptyView&gt;
            &lt;CollectionView.ItemTemplate&gt;
                &lt;DataTemplate x:DataType=&quot;models:Player&quot;&gt;
                    &lt;Border 
                        BackgroundColor=&quot;{StaticResource Secondary}&quot;
                        Margin=&quot;10,10,10,10&quot;
                        HeightRequest=&quot;120&quot;
                        Padding=&quot;20&quot;&gt;
                        &lt;Border.StrokeShape&gt;
                            &lt;RoundRectangle CornerRadius=&quot;10&quot; &gt;&lt;/RoundRectangle&gt;
                        &lt;/Border.StrokeShape&gt;
                        &lt;HorizontalStackLayout&gt;
                            &lt;Label Text=&quot;{Binding Name}&quot;
                                   FontSize=&quot;18&quot; 
                                   MaximumWidthRequest=&quot;120&quot;
                                   HorizontalTextAlignment=&quot;Center&quot;
                                   VerticalOptions=&quot;Start&quot;
                                   MaxLines=&quot;3&quot;  
                                   Margin=&quot;0,20,0,0&quot;
                                   FontAttributes=&quot;Bold&quot;
                                   TextColor=&quot;Black&quot;
                                   HorizontalOptions=&quot;Center&quot;/&gt;
                        &lt;/HorizontalStackLayout&gt;
                    &lt;/Border&gt;
                &lt;/DataTemplate&gt;
            &lt;/CollectionView.ItemTemplate&gt;
        &lt;/CollectionView&gt;

        &lt;ActivityIndicator
            IsVisible=&quot;{Binding IsBusy}&quot;
            IsRunning=&quot;{Binding IsBusy}&quot;
            Grid.RowSpan=&quot;2&quot;
            Grid.ColumnSpan=&quot;2&quot;
            BackgroundColor=&quot;Transparent&quot;
            Color=&quot;{StaticResource Primary}&quot;
            HeightRequest=&quot;100&quot;
            WidthRequest=&quot;100&quot;
            HorizontalOptions=&quot;FillAndExpand&quot;
            VerticalOptions=&quot;CenterAndExpand&quot;/&gt;

    &lt;/Grid&gt;
&lt;/ContentPage&gt;

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&lt;Team&gt; Teams { get; set; } = new();
        public ObservableCollection&lt;Player&gt; Players { get; set; } = new();

        [ObservableProperty]
        Team selectedTeam;

        [RelayCommand]
        async void UpdateGroupList() =&gt; await GetTeams();

        [RelayCommand]
        async void UpdateMembersList() =&gt; await GetPlayers();

        public async Task GetTeams()
        {
            IsBusy = true;
            var cources = await _teamService.GetAllAsync();
            await MainThread.InvokeOnMainThreadAsync(() =&gt;
            {
                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(() =&gt;
            {
                Players.Clear();
                foreach (var player in players)
                    Players.Add(player);
            });
            IsBusy = false;
        }
        
    }
}

error:
error XFC0000 Cannot resolve type &quot;using:_153502_Tolstoi.Domain.Entities:models:Player&quot;. 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=&quot;clr-namespace:Behaviors;assembly=BehaviorsLibrary&quot;

huangapple
  • 本文由 发表于 2023年5月11日 04:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定