Why is my ViewModel not found?

牧云@^-^@ 提交于 2019-12-07 20:38:44

问题


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; }
    }
}

回答1:


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.




回答2:


You don't use using in xaml, you use clr-namespace:. I can't be sure of what you named your assembly, but my guess is:

xmlns:viewModels="clr-namespace:reOrder;assembly=reOrder"



回答3:


It happened after I set compile target to x64. My view model can't be found until I reset compile to Any CPU.



来源:https://stackoverflow.com/questions/38934781/why-is-my-viewmodel-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!