Ste*_*awg 7 wpf mvvm-light portable-class-library
大家都是SO观众.我通常是Android开发人员,但现在我正在开发针对WPF和Android的跨平台应用程序.话虽如此,几乎没有关于如何直接做我想要的信息.因此,我最终找到了一个由3部分组成的博客系列,深入探讨了如何开发基于Windows的跨平台MVVM项目.只要我将PCL设置为与Xamarin.Android兼容,任何不会抛出错误的代码应该在我进入Android方面时起作用.以下是博客文章的链接:博客1,博客2,博客3.我再做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.在GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(键入serviceType,String key,Boolean cache)中的c:\ MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras(PCL)\ Ioc\SimpleIoc.cs:第537行at GalaSoft.MvvmLight.Iv.SimpleIoc.GetService(类型serviceType)位于c:\ MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras(PCL)\ Ioc\SimpleIoc.cs:第789行,位于GalaSoft.MvvmLight.Ioc.SimpleIoc .MakeInstanceTClass在c:\ MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras(PCL)\ Ioc\SimpleIoc.cs:line 729} System.Exception {Microsoft.Practices.ServiceLocation.ActivationException}
你们有什么可以告诉我的,也许博客作者可能会跳过我需要做的事情吗?也许如果在这块"巨石"上扔出足够的岩石,它就会开裂......
我的Visual Studio解决方案目前基本上只有两个项目.一个是可移植类库.另一个是WPF应用程序.在不久的将来,一旦我开始在WPF方面工作,我将使用Xamarin中的PCL来重用Android项目中的代码.但是,Android方面不是我的问题的一部分.我只处理WPF项目时遇到了上述问题.
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
MainViewModel.cs
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
}
}
Run Code Online (Sandbox Code Playgroud)
LocatorService.cs(位于WPF项目中的DeviceLocatorService)
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackOverF.Services {
public class DeviceLocatorService {
static DeviceLocatorService() {
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic) {
}
else {
}
if (!SimpleIoc.Default.IsRegistered<IStorageService>())
SimpleIoc.Default.Register<IStorageService, StorageService>();
}
public static void Cleanup() {
}
}
}
Run Code Online (Sandbox Code Playgroud)
LocatorService.cs(LocatorService,位于PCL项目中)
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using StackOverF.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackOverF.Services {
public class LocatorService {
static LocatorService() {
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
// Services
if (ViewModelBase.IsInDesignModeStatic) {
SimpleIoc.Default.Register<IDataService, Design.DataService>();
}
else {
SimpleIoc.Default.Register<IDataService, Services.DataService>();
}
// View Models
SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();
}
public IMainViewModel MainViewModel {
get {
return ServiceLocator.Current.GetInstance<IMainViewModel>();
}
}
public static void Cleanup() {
}
}
}
Run Code Online (Sandbox Code Playgroud)
它出错了(仅限于调试)return ServiceLocator.Current.GetInstance<IMainViewModel>();.
App.xaml中
<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>
Run Code Online (Sandbox Code Playgroud)
我的问题的解决方案比人们想象的要简单。WPF应用程序使用工具包/框架没有问题MVVM,但它们似乎在共享方面存在问题。由于WPF它并不是一种跨平台友好的语言,因此“正确”的编程方式不适用于它。
问题是试图将这两个LocatorService类都包含在 中App.xaml,并期望像或可能WPF那样运行这两个类。似乎只在需要数据时才引用一个类。就像博客的示例一样,我将数据绑定到了类。由于应用程序只运行该类的代码,因此它会抛出错误。WinRTWinPhoneWPFMain.xamlLocatorServiceWPF
解决方案是LocatorService在项目方面将这些文件合并为一个文件WPF。WPF你问为什么在一边?APortable Class Library应该只包含通用代码,可跨平台共享。
| 归档时间: |
|
| 查看次数: |
2004 次 |
| 最近记录: |