C# ';System.Reflection.TargetingException';(内部MVVM灯)
我使用了MVVM Light toolkit,当我在ViewModelLocator中加载ViewModel实例时,我得到一个异常,在mscorlib.ni.dll中发生了类型为“System.Reflection.TargetInvocationException”的异常,但未在用户代码中处理。我找了很多,但还没找到解决办法 我的ViewModelLocator代码:C# ';System.Reflection.TargetingException';(内部MVVM灯),c#,windows-phone-8,mvvm,mvvm-light,C#,Windows Phone 8,Mvvm,Mvvm Light,我使用了MVVM Light toolkit,当我在ViewModelLocator中加载ViewModel实例时,我得到一个异常,在mscorlib.ni.dll中发生了类型为“System.Reflection.TargetInvocationException”的异常,但未在用户代码中处理。我找了很多,但还没找到解决办法 我的ViewModelLocator代码: /* In App.xaml: <Application.Resources> <
/*
In App.xaml:
<Application.Resources>
<vm:ViewModelLocator xmlns:vm="clr-namespace:ToDoList"
x:Key="Locator" />
</Application.Resources>
In the View:
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
You can also use Blend to do all this with the tool's support.
See http://www.galasoft.ch/mvvm
*/
using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Practices.ServiceLocation;
using System.Windows;
namespace ToDoList.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view services and models
//// SimpleIoc.Default.Register<IDataService, DesignDataService>();
////}
////else
////{
//// // Create run time view services and models
//// SimpleIoc.Default.Register<IDataService, DataService>();
////}
if (!SimpleIoc.Default.IsRegistered<IMarketplaceReviewService>())
{
SimpleIoc.Default.Register<IMarketplaceReviewService, MarketplaceReviewService>();
}
SimpleIoc.Default.Register<ToDoViewModel>();
}
public ToDoViewModel ToDo
{
get
{
return ServiceLocator.Current.GetInstance<ToDoViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];
viewModelLocator.ToDo.Cleanup();
Messenger.Reset();
}
}
}
/*
在App.xaml中:
他认为:
DataContext=“{Binding Source={StaticResource Locator},Path=ViewModelName}”
您还可以使用Blend在工具的支持下完成所有这一切。
看见http://www.galasoft.ch/mvvm
*/
使用Cimbalino.Phone.Toolkit.Services;
使用GalaSoft.MvvmLight;
使用GalaSoft.MvvmLight.Ioc;
使用GalaSoft.MvvmLight.Messaging;
使用Microsoft.Practices.ServiceLocation;
使用System.Windows;
命名空间ToDoList.ViewModel
{
///
///此类包含对中所有视图模型的静态引用
///应用程序,并为绑定提供入口点。
///
公共类ViewModelLocator
{
///
///初始化ViewModelLocator类的新实例。
///
公共ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
////if(ViewModelBase.IsIndesignatic)
////{
//////创建设计时视图服务和模型
////SimpleIoc.Default.Register();
////}
////否则
////{
//////创建运行时视图服务和模型
////SimpleIoc.Default.Register();
////}
如果(!SimpleIoc.Default.IsRegistered())
{
SimpleIoc.Default.Register();
}
SimpleIoc.Default.Register();
}
公共ToDo提供模型ToDo
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
公共静态无效清除()
{
//要清除ViewModels,请执行以下操作:
var viewModelLocator=(viewModelLocator)Application.Current.Resources[“Locator”];
viewModelLocator.ToDo.Cleanup();
Messenger.Reset();
}
}
}
我的ToDoViewModel:
using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ToDoList.Models;
namespace ToDoList.ViewModel
{
public class ToDoViewModel : ViewModelBase
{
private readonly IMarketplaceReviewService _marketplaceReviewService;
public ICommand DeleteCommand { get; private set; }
public ICommand AddCommand { get; set; }
public ICommand RateCommand { get; private set; }
public string Text { get; set; }
// LINQ to SQL data context for the local database.
private ToDoDataContext toDoDB;
// Class constructor, create the data context object.
public ToDoViewModel(string toDoDBConnectionString, IMarketplaceReviewService marketplaceReviewService)
{
_marketplaceReviewService = marketplaceReviewService;
toDoDB = new ToDoDataContext(toDoDBConnectionString);
DeleteCommand = new RelayCommand<ToDoItem>(DeleteToDoItem);
AddCommand = new RelayCommand(Add);
LoadCollectionsFromDatabase();
RateCommand = new RelayCommand(Rate);
}
private void Rate()
{
_marketplaceReviewService.Show();
}
private void Delete(ToDoItem newToDoItem)
{
//ToDoItem newToDoItem = obj as ToDoItem;
DeleteToDoItem(newToDoItem);
}
private void Add()
{
ToDoItem newToDoItem = new ToDoItem
{
ItemName = this.Text,
};
AddToDoItem(newToDoItem);
}
//
// TODO: Add collections, list, and methods here.
//
// Write changes in the data context to the database.
public void SaveChangesToDB()
{
toDoDB.SubmitChanges();
}
// All to-do items.
private ObservableCollection<ToDoItem> _allToDoItems;
public ObservableCollection<ToDoItem> AllToDoItems
{
get { return _allToDoItems; }
set
{
_allToDoItems = value;
NotifyPropertyChanged("AllToDoItems");
}
}
public void LoadCollectionsFromDatabase()
{
// Specify the query for all to-do items in the database.
var toDoItemsInDB = from ToDoItem todo in toDoDB.Items
select todo;
// Query the database and load all to-do items.
AllToDoItems = new ObservableCollection<ToDoItem>(toDoItemsInDB);
// Specify the query for all categories in the database.
}
// Add a to-do item to the database and collections.
public void AddToDoItem(ToDoItem newToDoItem)
{
// Add a to-do item to the data context.
toDoDB.Items.InsertOnSubmit(newToDoItem);
// Save changes to the database.
toDoDB.SubmitChanges();
// Add a to-do item to the "all" observable collection.
AllToDoItems.Add(newToDoItem);
}
// Remove a to-do task item from the database and collections.
public void DeleteToDoItem(ToDoItem toDoForDelete)
{
// Remove the to-do item from the "all" observable collection.
AllToDoItems.Remove(toDoForDelete);
// Remove the to-do item from the data context.
toDoDB.Items.DeleteOnSubmit(toDoForDelete);
// Save changes to the database.
toDoDB.SubmitChanges();
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify the app that a property has changed.
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
使用Cimbalino.Phone.Toolkit.Services;
使用GalaSoft.MvvmLight;
使用GalaSoft.MvvmLight.Command;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Input;
使用ToDoList.Models;
命名空间ToDoList.ViewModel
{
提供模型的公共类:ViewModelBase
{
私有只读IMarketplaceReviewService\u marketplaceReviewService;
公共ICommand DeleteCommand{get;private set;}
公共ICommand AddCommand{get;set;}
public ICommand rate命令{get;private set;}
公共字符串文本{get;set;}
//本地数据库的LINQ到SQL数据上下文。
私有ToDoDataContext toDoDB;
//类构造函数,创建数据上下文对象。
public-to-oviewModel(string-to-odbConnectionString,IMarketplaceReviewService-marketplaceReviewService)
{
_marketplaceReviewService=marketplaceReviewService;
toDoDB=新ToDoDataContext(toDoDBConnectionString);
DeleteCommand=新的RelayCommand(DeleteToDoItem);
AddCommand=新的RelayCommand(添加);
LoadCollectionsFromDatabase();
RateCommand=新的RelayCommand(速率);
}
私人空置率()
{
_marketplaceReviewService.Show();
}
私有作废删除(ToDoItem newToDoItem)
{
//ToDoItem newToDoItem=对象作为ToDoItem;
DeleteToDoItem(新ToDoItem);
}
私有void Add()
{
ToDoItem newToDoItem=新ToDoItem
{
ItemName=this.Text,
};
AddToDoItem(新的ToDoItem);
}
//
//TODO:在此处添加集合、列表和方法。
//
//将数据上下文中的更改写入数据库。
public void SaveChangesToDB()
{
toDoDB.SubmitChanges();
}
//所有待办事项。
私人可观察收集所有文件;
公共观察收集所有待办事项
{
获取{return\u allToDoItems;}
设置
{
_allToDoItems=值;
NotifyPropertyChanged(“AllToDoItems”);
}
}
公共void LoadCollectionsFromDatabase()
{
//指定数据库中所有待办事项的查询。
var toDoItemsInDB=从ToDoItem todo到toDoDB.Items
选择待办事项;
//查询数据库并加载所有待办事项。
AllToDoItems=新的ObservableCollection(toDoItemsInDB);
//指定数据库中所有类别的查询。
}
//将待办事项添加到数据库和集合中。
公共作废AddToDoItem(ToDoItem newToDoItem)
{
//将待办事项添加到数据上下文中。
toDoDB.Items.InsertOnSubmit(新TODOItem);
//将更改保存到数据库。
toDoDB.SubmitChanges();
//将待办事项添加到“所有”可观察集合。
所有ToDoItems.Add(新ToDoItem);
}
//从数据库和集合中删除待办任务项。
公共作废删除ToDoItem(ToDoItem toDoForDelete)
{
//从“所有”可观察集合中删除待办事项。
所有TODOItems.Remove(TODOOFORDELETE);
//从数据上下文中删除待办事项。
toDoDB.Items.deleteosubmit(todofoordelete);
//将更改保存到数据库。
toDoDB.SubmitChanges(
public ToDoViewModel(IMarketplaceReviewService marketplaceReviewService)
{
// Save your connection string somewhere in Constant Class
// Use that constants directly here.
_toDoDbConnectionString = "Your Connection string";
...
}