0

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)
        {
            ViewToPrint.NavigateToString(htmlSource);

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

            printDoc = new PrintDocument();

            try
            {
                printDoc.GetPreviewPage += PrintDoc_GetPreviewPage;
                printDoc.Paginate += PrintDoc_Paginate;
                printDoc.AddPages += PrintDoc_AddPages;

                var showprint = await PrintManager.ShowPrintUIAsync();

            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }

            PrintDoc = null;
            GC.Collect();
        }

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

            deff.Complete();

        }
        async void OnPrintTaskSourceRequested(PrintTaskSourceRequestedArgs args)
        {
            var def = args.GetDeferral();
            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                args.SetSource(printDoc.DocumentSource);
            });
            def.Complete();
        }

        private void PrintDoc_AddPages(object sender, AddPagesEventArgs e)
        {
            printDoc.AddPage(ViewToPrint);
            printDoc.AddPagesComplete();
        }

        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

4
  • is the interface defined in the shared project? Have you read the docs and samples for using DependencyService?
    – Jason
    Mar 17, 2021 at 1:46
  • No, is not defined in shared project, how can I define? can you share with me that documentation please? @Jason
    – Jason
    Mar 17, 2021 at 2:39
  • 1
    Literally move the interface file in the shared project. The docs are the first hit on Google - learn.microsoft.com/en-us/xamarin/xamarin-forms/…
    – Jason
    Mar 17, 2021 at 2:44
  • Awesome, thanks! @Jason
    – Jason
    Mar 17, 2021 at 4:39

1 Answer 1

0

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))]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.