我正在使用 Xamarin Forms 开发一个 iOS 应用程序,为此我创建了一个核心模型,所有应用程序功能都围绕该模型工作。
我想包含一个简单的 watchOS 应用程序,它允许用户在任何时候对该模型的单个实例进行操作。我已经使用 WCSession (通过这个WCSessionManager 类)实现了一些代码来更新 watchOS 应用程序中的模型。我还重用了一些代码来实现我的 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 是什么以及它为什么会抛出这个错误?如果不允许,在我的电话应用程序和我的手表应用程序之间共享这种代码的推荐解决方案是什么?我可以把它放在 PCL 中吗?我应该在项目之间复制代码吗?