Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVVM灯-PCL和x2B;WPF-获取类型为Microsoft.Practices.ServiceLocation.ActivationException的异常 背景_Wpf_Mvvm Light_Portable Class Library - Fatal编程技术网

MVVM灯-PCL和x2B;WPF-获取类型为Microsoft.Practices.ServiceLocation.ActivationException的异常 背景

MVVM灯-PCL和x2B;WPF-获取类型为Microsoft.Practices.ServiceLocation.ActivationException的异常 背景,wpf,mvvm-light,portable-class-library,Wpf,Mvvm Light,Portable Class Library,观众好。我通常是一名Android开发人员,但现在我正在开发一个针对WPF和Android的跨平台应用程序。也就是说,实际上没有关于如何直接做我想做的事情的信息。因此,我最终找到了一个由三部分组成的博客系列,深入讨论了如何开发基于Windows的跨平台MVVM项目。只要我将PCL设置为与Xamarin.Android兼容,任何不抛出错误的代码在我进入Android端后都应该可以工作。以下是博客帖子的链接:。同样,我使用Android,所以我对为WPF应用程序编写代码还不熟悉 问题 因此,我今天的

观众好。我通常是一名Android开发人员,但现在我正在开发一个针对WPF和Android的跨平台应用程序。也就是说,实际上没有关于如何直接做我想做的事情的信息。因此,我最终找到了一个由三部分组成的博客系列,深入讨论了如何开发基于Windows的跨平台MVVM项目。只要我将PCL设置为与Xamarin.Android兼容,任何不抛出错误的代码在我进入Android端后都应该可以工作。以下是博客帖子的链接:。同样,我使用Android,所以我对为WPF应用程序编写代码还不熟悉

问题 因此,我今天的问题只涉及PCL-WPF方面,这与上面链接的博客文章有关。我尽我所能地遵循岗位上的每一步。该博客使用WinRT和WinPhone作为两个目标平台,因此我必须自己尝试解决问题,以便在WPF上工作。我必须做的两件事是使用
IsolatedStorage
和基本上使用WinPhone
App.Xaml
进行WPF端构建

我已经完成了博客的所有方式结束和建设成功。我甚至可以看到我的示例调试行,就像它在第三篇博文末尾所说的那样。但是,当我运行它时,我得到以下信息:

用户代码未处理ActivationException

GalaSoft.MvvmLight.Extras.dll中发生类型为“Microsoft.Practices.ServiceLocation.ActivationException”的异常,但未在用户代码中处理

$exception{Microsoft.Practices.ServiceLocation.ActivationException:在缓存中找不到类型:StackOverF.Services.IStorageService。 在c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras(PCL)\Ioc\SimpleIoc.cs中的GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(类型serviceType,字符串键,布尔缓存)中 在c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras(PCL)\Ioc\SimpleIoc.cs中的GalaSoft.MvvmLight.Ioc.SimpleIoc.GetService(类型serviceType)中 在c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras(PCL)\Ioc\SimpleIoc.cs:line 729}系统中的GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstanceClass处。异常{Microsoft.Practices.ServiceLocation.ActivationException}

你们有什么可以告诉我的,也许博客作者跳过了我需要做的吗?也许如果有足够多的石头扔到这块“巨石”上,它就会裂开

澄清 在我的VisualStudio解决方案中,目前基本上只有两个项目。一个是可移植类库。另一个是WPF应用程序。在不久的将来,一旦我在WPF方面取得进展,我将使用Xamarin中的PCL在Android项目中重用代码。然而,Android方面并不是我的问题的一部分。我在处理WPF项目时遇到了上述问题

代码(最后编辑日期:2016年2月18日) IMainViewModel.cs

using GalaSoft.MvvmLight.Command;
using StackOverF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackOverF.ViewModels {
    public interface IMainViewModel {
        ObservableCollection<Workload> Workload { get; }

        RelayCommand RefreshCommand { get; }
        RelayCommand AddCommand { get; }
    }
}
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using StackOverF.Models;
using StackOverF.Services;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace StackOverF.ViewModels {
    public class MainViewModel : ViewModelBase,IMainViewModel {

        private IDataService dataService;

        public MainViewModel(IDataService dataService) {
            this.dataService = dataService;

            RefreshAsync();
        }

        private ObservableCollection<Workload> workload = new ObservableCollection<Workload>();
        public ObservableCollection<Workload> Workload {
            get {
                return workload;
            }
        }

        #region Commands

        #region Refresh
        private RelayCommand refreshCommand;
        public RelayCommand RefreshCommand {
            get {
                return refreshCommand ?? (refreshCommand = new RelayCommand(async () => { await RefreshAsync();}));
            }
        }

        private async Task RefreshAsync() {
            workload.Clear();
            foreach (Workload listing in await dataService.GetWorkloadAsync()) {
                workload.Add(listing);
            }
        }
        #endregion

        #region Add
        private RelayCommand addCommand;
        public RelayCommand AddCommand {
            get {
                return addCommand ?? 
                    (addCommand = new RelayCommand(async () => { 
                        Workload listing = new Workload() { Id = 3, Serial = "relay12" };
                        await dataService.AddWorkloadAsync(listing);
                        workload.Add(listing);
                    }));
            }
        }
        #endregion

        #endregion
    }
}
使用GalaSoft.MvvmLight.Command;
使用StackOverF.模型;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间StackOverF.ViewModels{
公共接口IMainViewModel{
ObservableCollection工作负载{get;}
RelayCommand刷新命令{get;}
RelayCommand AddCommand{get;}
}
}
MainViewModel.cs

using GalaSoft.MvvmLight.Command;
using StackOverF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackOverF.ViewModels {
    public interface IMainViewModel {
        ObservableCollection<Workload> Workload { get; }

        RelayCommand RefreshCommand { get; }
        RelayCommand AddCommand { get; }
    }
}
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using StackOverF.Models;
using StackOverF.Services;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace StackOverF.ViewModels {
    public class MainViewModel : ViewModelBase,IMainViewModel {

        private IDataService dataService;

        public MainViewModel(IDataService dataService) {
            this.dataService = dataService;

            RefreshAsync();
        }

        private ObservableCollection<Workload> workload = new ObservableCollection<Workload>();
        public ObservableCollection<Workload> Workload {
            get {
                return workload;
            }
        }

        #region Commands

        #region Refresh
        private RelayCommand refreshCommand;
        public RelayCommand RefreshCommand {
            get {
                return refreshCommand ?? (refreshCommand = new RelayCommand(async () => { await RefreshAsync();}));
            }
        }

        private async Task RefreshAsync() {
            workload.Clear();
            foreach (Workload listing in await dataService.GetWorkloadAsync()) {
                workload.Add(listing);
            }
        }
        #endregion

        #region Add
        private RelayCommand addCommand;
        public RelayCommand AddCommand {
            get {
                return addCommand ?? 
                    (addCommand = new RelayCommand(async () => { 
                        Workload listing = new Workload() { Id = 3, Serial = "relay12" };
                        await dataService.AddWorkloadAsync(listing);
                        workload.Add(listing);
                    }));
            }
        }
        #endregion

        #endregion
    }
}
使用GalaSoft.MvvmLight;
使用GalaSoft.MvvmLight.Command;
使用StackOverF.模型;
使用StackOverF.服务;
使用制度;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间StackOverF.ViewModels{
公共类MainViewModel:ViewModelBase、IMainViewModel{
私有IDataService数据服务;
公共主视图模型(IDataService数据服务){
this.dataService=dataService;
刷新异步();
}
私有ObservableCollection工作负载=新ObservableCollection();
公共可观测收集工作量{
得到{
返回工作量;
}
}
#区域命令
#区域刷新
专用中继命令刷新命令;
公共RelayCommand刷新命令{
得到{
返回refreshCommand??(refreshCommand=newrelaycommand(async()=>{await RefreshAsync();}));
}
}
专用异步任务RefreshAsync(){
工作量。清除();
foreach(await dataService.GetWorkloadAsync()中的工作负载列表){
工作量。添加(列表);
}
}
#端区
#区域添加
专用RelayCommand addCommand;
公共RelayCommand AddCommand{
得到{
返回addCommand??
(addCommand=new RelayCommand(异步()=>{
工作负载列表=新工作负载(){Id=3,Serial=“relay12”};
等待dataService.AddWorkloadAsync(列表);
工作量。添加(列表);
}));
}
}
#端区
#端区
}
}
LocatorService.cs(DeviceLocatorService,位于WPF项目中)

使用GalaSoft.MvvmLight;
使用GalaSoft.MvvmLight.Ioc;
使用Microsoft.Practices.ServiceLocation;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间StackOverF.Services{
公共类设备定位服务{
静态设备定位服务(){
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
if(ViewModelBase.IsIndesignatic){
}
否则{
}
如果(!
<Application x:Class="StackOverF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"   
             xmlns:services="clr-namespace:StackOverF.Services;assembly=StackOverF.PCL"
             xmlns:deviceServices="clr-namespace:StackOverF.Services"
             StartupUri="Views/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <deviceServices:DeviceLocatorService x:Key="Locator.WPF" d:IsDataSource="True" />
            <services:LocatorService x:Key="Locator" d:IsDataSource="True" />
        </ResourceDictionary>
    </Application.Resources>
</Application>
<Application
    x:Class="SampleApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:ignore="http://www.ignore.com"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
    mc:Ignorable="d ignore"    
    xmlns:services="using:SampleApp.Services"   
    xmlns:local="using:SampleApp">

    <Application.Resources>
        <ResourceDictionary>

            <services:LocatorService x:Key="Locator" d:IsDataSource="True" />
            <services:DeviceLocatorService x:Key="Locator.W8" d:IsDataSource="True" />
        </ResourceDictionary>
    </Application.Resources>
</Application>