Xamarin Forms 依赖服务定义问题

3
我正在使用Xamarin开发一个应用程序,我试图遵循这个指南来为Xamarin Forms实现UWP的接口。
因此,我在PCL中编写了这个接口:
namespace MyApp {
public interface ISimplePdfLoader {

    void OpenLocal(string uri);

    void Load(object pdfDoc);
  }
}

在MyApp.UWP中,我创建了一个类:
[assembly: Dependency(typeof(SimplePdfLoader))]
namespace MyApp.UWP {
public class SimplePdfLoader : ISimplePdfLoader {

    public async void OpenLocal(string uri) {
        ...

        Load(doc);
    }

    public async void Load(object pdfObj) {
        ...
        }
    }
}
}

但它继续显示错误 CS7036,“没有指定参数匹配 'DependencyAttribute.DependencyAttribute(string, LoadHint)' 的强制形式参数'loadHintArgument'”,我的 MyApp.UWP C:\Users...\workspace\my-app\MyApp\MyApp.UWP\SimplePdfLoader.cs 19,我无法编译该项目。
编辑:错误在以下行下面显示:[assembly: Dependency(typeof(SimplePdfLoader))]

你在App.xaml.cs文件中手动注册了每个依赖服务吗?参考:https://learn.microsoft.com/it-it/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction#universal-windows-platform-net-native-compilation - JKL
@JesseK 我尝试了,但它告诉我“在 App.xaml.cs 文件中,手动注册在 UWP 项目中定义的每个依赖服务”,但是我在 MyApp 和 MyApp.UWP 中都有一个 App.xaml.cs 文件,我应该把注册放在哪里? - Antonello Gatto
我相信它应该是你的UWP项目中的那个。 - JKL
@JesseK 它说要添加这一行:Xamarin.Forms.Forms.Init(e, assembliesToInclude); 但我不知道 assembliesToInclude 或 e 变量是什么。 - Antonello Gatto
1
你可能有一个using语句引用了System.Runtime.CompilerServices中的Dependency类,而不是Xamarin.Forms中的。 - Benl
显示剩余3条评论
2个回答

16

从顶部的“usings”部分中删除下面这行

using System.Runtime.CompilerServices;

并添加以下内容

using Xamarin.Forms;

1

[assembly: Dependency(typeof(SimplePdfLoader))]更改为[assembly: Xamarin.Forms.Dependency()]。您能看到可以向该Dependency对象提供哪些参数吗?我认为应该是这个[assembly: Xamarin.Forms.Dependency(typeof(SimplePdfLoader))]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接