如何调试 Xamarin watchOS 链接器错误

How can I debug a Xamarin watchOS Linker Error

我正在使用 Xamarin Forms 开发 iOS 应用程序,为此我创建了一个核心模型,所有应用程序功能都围绕该模型运行。

我想包括一个简单的 watchOS 应用程序,它允许用户在任何时候对该模型的单个实例进行操作。我已经使用 WCSession 实现了一些代码来更新 watchOS 应用程序中的模型(通过此 WCSessionManager Class)。我还重复使用了一些代码来实现我的 Xamarin Forms 项目中的计时器。

但是,我在构建解决方案时遇到了链接器错误。我想可能是因为我从我的 watchOS 项目中引用了我的 Xamarin Forms 项目,这可能是不允许的。这是错误:

/Users/luketimothy/Projects/TodoQ/TodoQ.Watch/TodoQ.Watch.WatchOSExtension/MTOUCH: Error MT2001: Could not link assemblies. Reason: Error while processing references of 'TodoQWatchWatchOSExtension, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' (MT2001) (TodoQ.Watch.WatchOSExtension)

错误引用的文件是 MTOUCH。我不确定这到底是什么,但在我的 watchOS 应用程序中,我引用 Xamarin Forms 代码的唯一地方是这个对象:

    using System;
    using System.Collections.Generic;
    using TodoQ.Models;
    using TodoQ.Utilities;
    using WatchConnectivity;
    using WatchKit;

    namespace TodoQ.Watch.WatchOSExtension
    {
        internal class TodoState
        {
            private TodoItem current;
            private ISessionTimer timer;

            public TodoItem Current { get => current; set { current = value; TaskUpdated(this, value); } }

            public event TaskUpdatedEventHandler TaskUpdated;
            public delegate void TaskUpdatedEventHandler(object sender, TodoItem current);

            public event TimerElapsedEventHandler TimerElapsed;
            public delegate void TimerElapsedEventHandler(object sender, TimerElapsedEventArgs current);

            public TodoState()
            {
                WCSessionManager.SharedManager.ApplicationContextUpdated += DidReceiveApplicationContext;

                timer = new PomodoroTimer();

                timer.ProgressUpdate += (object sender, ProgressUpdateEventArgs e) => 
                {
                    TimerElapsed(this, new TimerElapsedEventArgs() { Elapsed = e.Elapsed, EndTime = e.EndTime });
                };

                timer.MilestoneUpdate += (object sender, PomodoroStateID e) =>
                {
                    var audio_file = WKAudioFilePlayerItem.Create(WKAudioFileAsset.Create(new Foundation.NSUrl("ShortBreak.wav")));
                    var audio_player = WKAudioFilePlayer.Create(audio_file);
                    audio_player.Play();
                    WKInterfaceDevice.CurrentDevice.PlayHaptic(WKHapticType.Notification);
                };
            }

            public void DidReceiveApplicationContext(WCSession session, Dictionary<string, object> applicationContext)
            {
                var message = (TodoItem)applicationContext["FocusedItem"];
                if (message != null)
                {
                    Console.WriteLine($"Application context update received : {message.Heading}");
                    Current = message;
                }
            }

            public void StartTimer()
            {
                timer.StartSession();
            }
        }

        public class TimerElapsedEventArgs
        {
            public TimeSpan Elapsed;
            public TimeSpan EndTime;
        }
    }

所以,我的问题是。如果这应该被允许,并且错误是其他原因,我可以得到一些帮助来追踪这个 MTOUCH 是什么以及它为什么会抛出这个错误吗?如果不允许,在我的 Phone 应用程序和我的手表应用程序之间共享此类代码的推荐解决方案是什么?我可以把它放在 PCL 中吗?我应该在项目之间复制代码吗?

您不应将 WatchOS 项目引用到 Forms 项目。它应该直接添加到iOS项目中。

并且如果你想定义一些通用代码以供重用。您可以创建一个共享库:

在那里添加一些public类:

namespace UtiLibrary
{
    public static class UtiClass
    {
        public static List<Model> datas { get => new List<Model> { new Model { Name = "name" } }; }
    }

    public class Model
    {
        public string Name { set; get; }
    }
}

然后你就可以在每个引用了这个库的平台上使用它了。