Forum Xamarin.Mac


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

KeyPathsForValuesAffectingPreview Usage example

ShammyLevvaShammyLevva Member ✭✭
edited December 2018 in Xamarin.Mac

Hi all,

In my app I’ve got a custom view with sub views for printing. Populating it can take a while so I’m doing that when the user clicks print. This means the height and position of the views are set by the ViewWillDraw method which is triggered by the PrintOperation.RunOperation();

It all works fine with one caveat, the preview pane doesn’t immediately refresh. When the built in print panel shows it is initial blank. Clicking anything (eg copies button) forces a refresh and it previews correctly. I’ve discovered I need to use the KeyPathsForValuesAffectingPreview to tell the preview to refresh when something changed eg: view.frame size.

If I comment out the overriding of the ViewWillDraw method in my view class then the print preview shows but the framesizes are all wrong.

My issue is that I’m new to Xamarin Mac development and the limited documention I can find is objective C or swift and that syntax is alien to me.

What I haven’t been able to determine is where do I put this KeyPathsForValuesAffectingPreview code, is it an override? There is an example at Controllers/Printing/PrintAccessoryViewController.swift

But that’s a print accessory which I don’t think I need and anyway I can fathom how to convert the syntax to equivalent c# syntax. It seems to be an extension but I’m not sure of which class to put it in as an extension nor the right syntax for declaring such an extension.

So my printing is working but I need to tell the built in print panel to refresh using KeyPathsForValuesAffectingPreview. Can anyone enlighten me with a sample of how to do this please?


  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    edited December 2018

    So I have never used any of these APIs but the stack overflow links to this documentation:

    But as you noted, reading the Apple documentation requires parsing Objective-C. One challenge as a macOS application developer is being able to leverage the large number of resources out there not written in C#. A long time ago I wrote up some documentation that may be useful, including a "Reading enough Objective-C to be dangerous" section:

    A quick read of that document suggests that you will need to override NSSet GetKeyPathsForValuesAffecting (NSString key);

  • ShammyLevvaShammyLevva Member ✭✭
    edited December 2018

    Yes The NSSet KeyPathsForValuesAffectingPreview function noted in that document is a special case as it references a system window rather than any custom KVO implementation I might write.

    Your documentation may well be really useful as the objective C (and occasionally swift) can be tricky to parse when you aren't used to the syntax especially for more exotic things. I'm happy not to be competent in objective C, achieving the level of dangerous will be sufficient for my needs :smile:

    I'm going to attempt to write a custom print accessory as that is what all the examples include. I had hoped someone had trodden this path before and could give me an example or two, as the learning curve is already steep.

    I'll post back any code I get working to assist future googlers.

Sign In or Register to comment.