Paul Sinnema
Paul Sinnema

Reputation: 2792

Can not resolve reference: `Windows.Foundation.UniversalApiContract` in Android Application

In my Xamarin.Forms Application for UWP, Android and iOS (unloaded for the moment) I added the following code:

  StorageFolder localFolder = ApplicationData.Current.LocalFolder;

  databasePath = Path.Combine(localFolder.Path, databaseName);

This resulted in the addition of a reference in the .Net Standard library as follows.

<Reference Include="Windows.Foundation.UniversalApiContract">
    <HintPath>..\..\..\..\..\Program Files (x86)\Windows Kits\10\References\10.0.16299.0\Windows.Foundation.UniversalApiContract\5.0.0.0\Windows.Foundation.UniversalApiContract.winmd</HintPath>
  </Reference>

This in turn resulted in an error on the Android Application with the following text:

Can not resolve reference: `Windows.Foundation.UniversalApiContract`, referenced by `eTabber`. Please add a NuGet package or assembly reference for `Windows.Foundation.UniversalApiContract`, or remove the reference to `eTabber`.

I do need the reference to Windows.Storage in order to be able to find the right path to the local storage in the UWP application. How do I solve this problem?

Upvotes: 0

Views: 1384

Answers (1)

Paul Sinnema
Paul Sinnema

Reputation: 2792

Ok, it's easy if you know what to do. The reference should not be in the main .Net Standard library but in the UWP Application. So I did the following.

In the DeviceService in the main .Net Standard library I created the following code:

public class DeviceService
{
    private const string databaseName = "eTabber.db";

    public static string StoragePath { get; set; }

    /// <summary>
    /// Return the device specific database path for SQLite
    /// </summary>
    public static string GetSQLiteDatabasePath()
    {
        return Path.Combine(StoragePath, databaseName);
    }
}

The static property StoragePath is then filled at the start of each Application (UWP, iOS or Android).

UWP in App.Xaml.cs:

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;

        DeviceService.StoragePath = StoragePath;
    }

    private string StoragePath => ApplicationData.Current.LocalFolder.Path;

Android in MainActivity.cs (haven't tested this yet):

    protected override void OnCreate(Bundle savedInstanceState)
    {
        DeviceService.StoragePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        ...

iOS in Main.cs (haven't tested this yet):

    static void Main(string[] args)
    {
        DeviceService.StoragePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

For at least UWP this construct works fine. Removed the reference from the .Net Standard library and the error disappeared.

Upvotes: 1

Related Questions