本文介绍了为什么没有找到我的ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么VS找不到我的文件?



VS错误图像和文本:





严重性代码说明项目文件行抑制状态
错误CS0246 无法找到类型或命名空间名称AudioFile(您是否缺少一个使用指令或汇编引用?)reOrder C:\Users\kloud\Documents\Visual Studio 2015\Projects\reOrder\reOrder\ReorderPage.xaml.cs 27 Active



我重新创建了项目,看看Visual Studio是否正在做一些我看不到/理解并广泛检查正在工作的代码(正确找到项目和绑定)以查看差异的位置。我找不到任何线索。



编辑:我在Windows 10周年纪念更新中使用Visual Studio 2015社区版。此外,在以前的项目中,VS没有找到并绑定到模型的麻烦。



下面的完整代码。



重新排序页面

 < 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模型
(skipped usings)

 命名空间reOrder.Models 
{
public class AudioItem
{
public string Path {get ;组; }

public string Name {get;组; }

public int Duration {get;组;
}
}


解决方案

即使重建解决方案,我也遇到了同样的问题。我做过的一种解决方法是转到文件浏览器中的.csproj文件,现在在itemgroup标签下编辑添加:

 < Compile Include =ViewModels\AudioItemModel .cs> 
< Dependent Upon> Reorder.xaml< / Dependent Upon>
< / Compile>

保存并关闭它,然后重建它,现在将检测viewmodel。


Why can't VS find my file?

VS Error Image and Text:

Severity Code Description Project File Line Suppression StateError 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; }
    }
}
解决方案

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.

这篇关于为什么没有找到我的ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 05:39