Call UWP service on Shared project


I have a Xamarin app where I implemented a service to do some printing. I create that service on UWP app because it needs some dependencies of it.

UWP Service:

 public class PrintUWPService
        PrintManager printmgr  PrintManager.GetForCurrentView();
        PrintDocument PrintDoc;
        PrintDocument printDoc;
        PrintTask Task;
        private Windows.UI.Xaml.Controls.WebView ViewToPrint  new Windows.UI.Xaml.Controls.WebView();

        public PrintUWPService()
            printmgr.PrintTaskRequested + Printmgr_PrintTaskRequested;

        public async void Print(WebView viewToPrint, string htmlSource)

            if (PrintDoc ! null)
                printDoc.GetPreviewPage - PrintDoc_GetPreviewPage;
                printDoc.Paginate - PrintDoc_Paginate;
                printDoc.AddPages - PrintDoc_AddPages;

            printDoc  new PrintDocument();

                printDoc.GetPreviewPage + PrintDoc_GetPreviewPage;
                printDoc.Paginate + PrintDoc_Paginate;
                printDoc.AddPages + PrintDoc_AddPages;

                var showprint  await PrintManager.ShowPrintUIAsync();

            catch (Exception e)

            PrintDoc  null;

        private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
            var deff  args.Request.GetDeferral();
            Task  args.Request.CreatePrintTask("Grocery List", OnPrintTaskSourceRequested);


        async void OnPrintTaskSourceRequested(PrintTaskSourceRequestedArgs args)
            var def  args.GetDeferral();
            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () >

        private void PrintDoc_AddPages(object sender, AddPagesEventArgs e)

        private void PrintDoc_Paginate(object sender, PaginateEventArgs e)
            PrintTaskOptions opt  Task.Options;
            printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);

        private void PrintDoc_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
            printDoc.SetPreviewPage(e.PageNumber, ViewToPrint);

Then I create interface for it:

 public interface IPrintUWPService
        void Print(WebView viewToPrint, string htmlSource);

Now inside a class of my shared project I want to call this service as:

private readonly IPrintUWPService _printService   DependencyService.Get<IPrintUWPService>();

But it does not work. it says:

IPrintUWPService does not exist in the current context

So I try to access to UWP class as:

using MyCompany.ProjectName.App.UWP

But it says that “UWP” namespace does not exist. What am I doing wrong? is not possible call service on UWP project inside shared project? Regards


Call UWP DependencyService on Shared project

As @Jason mentioned in above comment, you need declare the interface in the Xamarin Forms project and implement it in the UWP client project.

And please note, after implementing the interface in each platform project, the platform implementations must be registered with the DependencyService, so that Xamarin.Forms can locate them at runtime. Use DependencyAttribute like the following.

[assembly: Dependency(typeof(UWP Client namesapce))]

Answered By – Nico Zhu – MSFT

Leave a Comment