Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In SimpleToDoList second run, error "Unable to resolve type" pops out #110

Open
spinningwatt opened this issue Dec 11, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@spinningwatt
Copy link

Describe the bug

I follow the SimpleToDoList tutorial and encounter an error at "second run". I added code below into my MainWindow.axaml file:

<ItemsControl ItemsSource="{Binding ToDoItems}">
    <ItemsControl.ItemTemplate>
        <!--  The ItemTemplate defines how each item should be represented  -->
        <!--  Our Item will be represented by a CheckBox and a Delete-Button  -->
        <DataTemplate DataType="vm:ToDoItemViewModel">
            <Grid ColumnDefinitions="*, Auto">
                <CheckBox Content="{Binding Content}"
                          IsChecked="{Binding IsChecked}" />

                <!--  Note how we use the Root-Grid to get the MainViewModel here. As we use compiled bindings we have to cast the DataContext  -->
                <Button Command="{Binding #Root.((vm:MainViewModel)DataContext).RemoveItemCommand}"
                        CommandParameter="{Binding .}"
                        Grid.Column="1">
                    Delete
                </Button>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Then, full MainWindow.axaml file is:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:AvaloniaTodo.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:views="clr-namespace:AvaloniaTodo.Views"
        mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450"
        x:Class="AvaloniaTodo.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="AvaloniaTodo">
		<!-- We give a name to the root grid in order to access it later -->
		<Grid RowDefinitions="Auto, *, Auto"
			  x:Name="Root">

			<!-- This is our title text block.  -->
			<TextBlock Text="ToDo List App" />

			<ScrollViewer Grid.Row="1">
				<!-- This ItemsControl show all added ToDoItems. -->
				<!-- It needs to be placed inside a ScrollViewer because other than a ListBox it has not its own -->
				<ItemsControl ItemsSource="{Binding ToDoItems}">
					<ItemsControl.ItemTemplate>
						<!--  The ItemTemplate defines how each item should be represented  -->
						<!--  Our Item will be represented by a CheckBox and a Delete-Button  -->
						<DataTemplate DataType="vm:ToDoItemViewModel">
							<Grid ColumnDefinitions="*, Auto">
								<CheckBox Content="{Binding Content}"
										  IsChecked="{Binding IsChecked}" />

								<!--  Note how we use the Root-Grid to get the MainViewModel here. As we use compiled bindings we have to cast the DataContext  -->
								<Button Command="{Binding #Root.((vm:MainViewModel)DataContext).RemoveItemCommand}"
										CommandParameter="{Binding .}"
										Grid.Column="1">
									Delete
								</Button>
							</Grid>
						</DataTemplate>
					</ItemsControl.ItemTemplate>
				</ItemsControl>
			</ScrollViewer>

			<!-- This TextBox can be used to add new ToDoItems -->
			<TextBox Grid.Row="2"
					 Text="{Binding NewItemContent}"
					 Watermark="Add a new Item">
				<TextBox.InnerRightContent>
					<Button Command="{Binding AddItemCommand}" >
						Add
						<!-- <PathIcon Data="{DynamicResource AcceptIconData}" Foreground="Green" /> -->
					</Button>
				</TextBox.InnerRightContent>
				<!--  KeyBindings allow us to define keyboard gestures  -->
				<TextBox.KeyBindings>
					<KeyBinding Command="{Binding AddItemCommand}" Gesture="Enter" />
				</TextBox.KeyBindings>
			</TextBox>
		</Grid>
</Window>

When I click button "add", there's an error:

Unable to resolve type vm:MainViewModel from any of the following locations:

Sample affected

https://github.com/AvaloniaUI/Avalonia.Samples/tree/main/src/Avalonia.Samples/CompleteApps/SimpleToDoList

Proposed solution

It should be able to add a new todo item into the list.

Screenshots

image

@spinningwatt spinningwatt added the bug Something isn't working label Dec 11, 2024
@timunie
Copy link
Collaborator

timunie commented Dec 11, 2024

Hey @spinningwatt depending on the IDE and template used to create your project, the name of the MainViewModel may be MainViewModel or MainWindowViewModel. Make sure it matches on your side.

@spinningwatt
Copy link
Author

Hey @timunie , I make sure there is a MainViewModel.cs file in my project.
image

@timunie
Copy link
Collaborator

timunie commented Dec 24, 2024

Please delete bin and obj folders and upload the code to GitHub. Preferably to a new repo and share the link or if that's not working for you via zip file here. Will take a look latest in January.

Something is screwed but I don't see it rn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants