当链接器设置为“全部链接”时,就会发生Xamarin错误。无法使用DependencyService [英] Xamarin bug happens when linker is set to "Link All". Can't use DependencyService

查看:120
本文介绍了当链接器设置为“全部链接”时,就会发生Xamarin错误。无法使用DependencyService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,由于UIWebView已过时,我必须将链接器设置为全部链接才能提交到App Store。在执行此操作时,我必须在我的数据存储中添加 [Preserve(AllMembers = true)] 才能使它们以这种方式工作,但后来遇到了这个问题。

Right now, I have to have my linker set to "Link All" in order to submit to the App Store because of the deprecated UIWebView. While doing this, I had to add [Preserve(AllMembers = true)] to my DataStores to make them work this way, but then I ran into this issue.

以下行将返回null:

The following line will return null:

var dp = DependencyService.Get<ITextMeter>();

研究解决方案后,似乎最好的答案是在AppDelegate中添加以下行:

After looking into the solution, it seemed the best answer would be to add the following line into the AppDelegate:

DependencyService.Register<ITextMeter, TextMeterImplementation>();

一旦这样做,我就开始收到此异常:

Once I did that, I started receiving this exception:

DependencyService:System.MissingMethodException:找不到[Interface]的默认构造函数
https://forums.xamarin.com/discussion/71072/dependencyservice-system-missingmethodexception-默认构造函数找不到接口

我只想找到一个可行的解决方案,该解决方案可使链接器设置为链接全部。

I just want to find a working solution that will allow everything to work with the linker set to "Link All". Thanks in advance.

ITextMeter:

ITextMeter:

using System;
using Xamarin.Forms.Internals;

namespace RedSwipe.Services
{
    public interface ITextMeter
    {
        double MeasureTextSize(string text, double width, double fontSize, string fontName = null);
    }
}

TextMeterImplementation:

TextMeterImplementation:

using System.Drawing;
using Foundation;
using RedSwipe.iOS.Services;
using RedSwipe.Services;
using UIKit;

[assembly: Xamarin.Forms.Dependency(typeof(TextMeterImplementation))]
namespace RedSwipe.iOS.Services
{
    public class TextMeterImplementation : ITextMeter
    {

        public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
        {
            var nsText = new NSString(text);
            var boundSize = new SizeF((float)width, float.MaxValue);
            var options = NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin;

            if (fontName == null)
            {
                fontName = "HelveticaNeue";
            }

            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName(fontName, (float)fontSize)
            };

            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            //return new Xamarin.Forms.Size((double)sizeF.Width, (double)sizeF.Height);
            return (double)sizeF.Height;
        }

    }
}


推荐答案

在实现类之前,将此 [Preserve(AllMembers = true)] 添加到您的 TextMeterImplementation 中。

Add this [Preserve (AllMembers = true)] in your TextMeterImplementation before class implementation.

您的代码应类似于:

using System.Drawing;
using Foundation;
using RedSwipe.iOS.Services;
using RedSwipe.Services;
using UIKit;
using Xamarin.Forms.Internals; // Add This import

[assembly: Xamarin.Forms.Dependency(typeof(TextMeterImplementation))]
namespace RedSwipe.iOS.Services
{
    [Preserve (AllMembers = true)]
    public class TextMeterImplementation : ITextMeter
    {

        public double MeasureTextSize(string text, double width, double fontSize, string fontName = null)
        {
            var nsText = new NSString(text);
            var boundSize = new SizeF((float)width, float.MaxValue);
            var options = NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin;

            if (fontName == null)
            {
                fontName = "HelveticaNeue";
            }

            var attributes = new UIStringAttributes
            {
                Font = UIFont.FromName(fontName, (float)fontSize)
            };

            var sizeF = nsText.GetBoundingRect(boundSize, options, attributes, null).Size;

            //return new Xamarin.Forms.Size((double)sizeF.Width, (double)sizeF.Height);
            return (double)sizeF.Height;
        }

    }
}

这篇关于当链接器设置为“全部链接”时,就会发生Xamarin错误。无法使用DependencyService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆