UWP无法将ViewModel添加到MainPage。找不到MainViewModel文件

时间:2017-06-07 22:05:04

标签: c# xaml mvvm uwp

这个问题让我抓狂。我的项目中有两个文件夹:Views和ViewModels。

在视图中我有我的MainPage.xaml 在ViewModel中,我有BaseViewModel.cs和MainViewModel.cs。

    class BaseViewModel:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void SetProperty<T>(ref T Storage, T Value, [CallerMemberName] string PropertyName = null)
    {
        if (Object.Equals(Storage, Value))
            return;
        else
        {
            Storage = Value;
        }

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }
}

和我的MainViewModel

    class MainViewModel:BaseViewModel
{
    //Bands
    public Tuple<List<Band>, List<Band>> BandsFromMobileAndDesktop { get; private set; }
    public List<List<Band>> BandsFormCSV { get; private set; }


    private ObservableCollection<Band> finalList;

    public ObservableCollection<Band> FinalList
    {
        get { return finalList; }
        set { SetProperty(ref finalList, value); }
    }


    //Properties for View
    private string text;
    public string Text
    {
        get { return text; }
        set { SetProperty(ref text, value); }
    }


    public MainViewModel()
    {

        Text = "Hurricane festival 2017";

        CreateObservableBandList();

        var result = DatabaseController.GetBandsInTimeFrame(new DateTime(2017, 06, 23, 16, 0, 0, 0), new DateTime(2017, 06, 23, 19, 0, 0, 0));
        var a = 0;
    }

    async public Task GetBands()
    {
        //Get Bands
        //First list from Mobile Second From Desktop
        BandsFromMobileAndDesktop = await BandsCollector.BandsCollector.GetBandDates();
        BandsFormCSV = await BandsCollector.BandsCollector.getBandsFromCSV();

        BandsCollector.BandsCollector.FillBandDataFromCSVWithImageByteArray(BandsFormCSV, BandsFromMobileAndDesktop);


        ApplicationData.Current.LocalSettings.Values["AlreadyStarted"] = "true";
    }

    public void CreateObservableBandList()
    {
        //Get Bands only on first start
        if (ApplicationData.Current.LocalSettings.Values["AlreadyStarted"] == null || ApplicationData.Current.LocalSettings.Values["AlreadyStarted"].ToString() != "true")
        {
            Task task = Task.Run(async () => { await GetBands(); });
            task.Wait();

            getFinalList(BandsFormCSV);

            DatabaseController.CreateDataBase();

            foreach (var bandFromList in FinalList)
            {
                DatabaseController.AddBand(bandFromList);
            }
        }

    }

    public async Task FillCollection(ObservableCollection<Band> dayCollection, List<Band> dayList)
    {
        for (int i = 0; i < dayList.Count; i++)
        {
            dayCollection.Add(dayList[i]);
        }
    }


    public void getFinalList(List<List<Band>> CSVList)
    {
        FinalList = new ObservableCollection<Band>();

        for (int day = 0; day < CSVList.Count; day++)
        {
            if (0 == day)
            {
                for (int band = 0; band < CSVList[day].Count; band++)
                {
                    CSVList[day][band].Day = day;
                    FinalList.Add(CSVList[day][band]);
                }
            }

            else if (1 == day)
            {
                for (int band = 0; band < CSVList[day].Count; band++)
                {
                    CSVList[day][band].Day = day;
                    FinalList.Add(CSVList[day][band]);
                }
            }

            else if (2 == day)
            {
                for (int band = 0; band < CSVList[day].Count; band++)
                {
                    CSVList[day][band].Day = day;
                    FinalList.Add(CSVList[day][band]);

                }
            }

            else if (3 == day)
            {
                for (int band = 0; band < CSVList[day].Count; band++)
                {
                    CSVList[day][band].Day = day;
                    FinalList.Add(CSVList[day][band]);
                }
            }
        }
    }
}

在我的MainPage.xaml中:

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

<Page.DataContext>
    <vm:MainViewModel/>
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

</Grid>

现在VS17说文件MainViewModel不存在。但是BaseViewModel可以工作。

我从另一个项目中复制了所有代码,我重新构建了所有代码,并且我使用了StackOverflow中的一些修补程序,这些修复程序最终无效。

我的错误是什么?我自己找不到。

谢谢

Agredo

0 个答案:

没有答案