C# UWP无法将ViewModel添加到主页面。找不到MainViewModel文件
这个问题让我发疯。我的项目中有两个文件夹:视图和视图模型 在视图中,我有我的MainPage.xaml 在ViewModel中,我有BaseViewModel.cs和MainViewModel.csC# UWP无法将ViewModel添加到主页面。找不到MainViewModel文件,c#,xaml,mvvm,uwp,C#,Xaml,Mvvm,Uwp,这个问题让我发疯。我的项目中有两个文件夹:视图和视图模型 在视图中,我有我的MainPage.xaml 在ViewModel中,我有BaseViewModel.cs和MainViewModel.cs class BaseViewModel:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void SetProperty<T>(r
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));
}
}
class BaseViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
public void SetProperty(ref T Storage,T Value,[CallerMemberName]string PropertyName=null)
{
if(Object.Equals(存储,值))
返回;
其他的
{
储存=价值;
}
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(PropertyName));
}
}
还有我的主视图模型
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]);
}
}
}
}
}
class MainViewModel:BaseViewModel
{
//乐队
公共元组带fromMobileAndesktop{get;private set;}
公共列表BandsFormCSV{get;private set;}
私人可观测集合财务师;
公共可观测集合财务师
{
获取{return finalList;}
集合{SetProperty(ref finalList,value);}
}
//用于查看的属性
私有字符串文本;
公共字符串文本
{
获取{返回文本;}
set{SetProperty(ref text,value);}
}
公共主视图模型()
{
Text=“2017年飓风节”;
CreateObservableBandList();
var结果=DatabaseController.GetBandsInTimeFrame(新日期时间(2017,06,23,16,0,0),新日期时间(2017,06,23,19,0,0);
var a=0;
}
异步公共任务GetBands()
{
//搞乐队
//第一个列表来自移动设备,第二个列表来自桌面
BandsFromobileAndDesktop=等待BandsCollector.BandsCollector.GetBandDates();
BandsFormCSV=等待BandsCollector.BandsCollector.getBandsFromCSV();
BandsCollector.BandsCollector.FillBandDatafromCSVhimageByTearray(BandsFormCSV,BandsFromMobileAndeSktop);
ApplicationData.Current.LocalSettings.Values[“AlreadyStarted”]=“true”;
}
public void CreateObservableHandList()
{
//仅在第一次开始时获得乐队
if(ApplicationData.Current.LocalSettings.Values[“AlreadyStarted”]==null | | ApplicationData.Current.LocalSettings.Values[“AlreadyStarted”].ToString()!=“true”)
{
Task Task=Task.Run(异步()=>{await GetBands();});
task.Wait();
getFinalList(BandsFormCSV);
DatabaseController.CreateDataBase();
foreach(FinalList中的var bandFromList)
{
DatabaseController.AddBand(bandFromList);
}
}
}
公共异步任务FillCollection(ObservableCollection dayCollection,List dayList)
{
对于(int i=0;i
在我的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我测试了您上面的代码片段,但无法重现您的问题。清洁和建造将会成功。你能提供吗?我正在使用vs17和uwp应用程序目标15063。您确定MainViewModel位于ViewModels命名空间中吗?我测试了您上面的代码片段,但无法重现您的问题。清洁和建造将会成功。你能提供最小的,完整的,可验证的例子吗?我正在使用vs17和uwp应用程序target 15063Sunteen Wu-MSFT 7小时前。--这听起来好像我的VisualStudio无法正常工作。就像我写的:我把我的整个项目复制到一个新项目中,清理并重建它。我将VS17与uwp应用程序目标15063和14393一起使用。键入