Kloudrazor Kloudrazor - 3 months ago 21
C# Question

Why is my ViewModel not found?

Why can't VS find my file? VS Error Image and Text:

Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'AudioFile' could not be found (are you missing a using directive or an assembly reference?) reOrder C:\Users\kloud\Documents\Visual Studio 2015\Projects\reOrder\reOrder\ReorderPage.xaml.cs 27 Active

I recreated the project to see if Visual Studio was doing something I couldn't see/understand and checked extensively with code that is working(correctly finds the item and binds) to see where the differences are. I couldn't find any clues though.

edit: I am using Visual Studio 2015 Community Edition on Windows 10 Anniversary Update. Also, in previous projects VS didn't have trouble finding and binding to a model.

Full code below.

Reorder Page

<Page
x:Class="reOrder.ReorderPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:reOrder"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="using:reOrder.Models"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>

<ListView Grid.Row="1"
ItemsSource="{x:Bind AudioFiles}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewModels:AudioItem">
<StackPanel>
<TextBlock Text="{x:Bind Name}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>


AudioItem Model
(skipped usings)

namespace reOrder.Models
{
public class AudioItem
{
public string Path { get; set; }

public string Name { get; set; }

public int Duration { get; set; }
}
}

Answer

I had the same problem even after rebuilding the solution.One workaround i did was Go to your .csproj file in file explorer and now edit under itemgroup tag add this:

<Compile Include="ViewModels\AudioItemModel .cs">
  <DependentUpon>Reorder.xaml</DependentUpon>
</Compile>

Save it and close it and then rebuild it and now it will detect the viewmodel.

Comments