Forum Xamarin.iOS

Announcement:

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.

StaticResource not found when using CreateViewController()

ntc4ntc4 Member ✭✭

I have created a share page in my forms app that needs to be implemented when sharing on Android and iOS. Everything works on Android using a share activity and CreateSupportFragment(). However, I've created an iOS share extension and I'm getting the following error in the device console when sharing on iOS:

Xamarin.Forms.Xaml.XamlParseException: Position 40:29. StaticResource not found for key backgroundGray

Nothing loads when selecting the app from the share sheet. Is there an extra step or something additional I need to initialize to use styles from App.xaml when using CreateViewController? Info.plist has NSExtensionPrincipalClass set to ShareExViewController and here is that class:

    [Register("ShareExViewController")]
    public class ShareExtensionViewController : UIViewController
    {
        ShareExtensionViewController()
        {
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Initialize Xamarin.Forms framework
            global::Xamarin.Forms.Forms.Init();

            FFImageLoading.Forms.Platform.CachedImageRenderer.Init();

            SharePage sharePage = new SharePage();
            var sharePageController = sharePage.CreateViewController();
            this.PresentModalViewController(sharePageController, false);
        }
    }

Best Answer

  • ntc4ntc4 Member ✭✭
    Accepted Answer

    To clarify, I created a Xamarin Forms app. One of the requirements of the app was to share files from other apps.

    On Android, I created a share activity with an intent filter to handle sharing. On iOS, I created a share extension.

    I was creating a support fragment out of a xamarin forms content page on Android as the UI for sharing. On iOS I was creating a view controller out of the same content page like here https://docs.microsoft.com/en-us/xamarin/ios/platform/extensions-with-xamarinforms.

    The issues was the content page (SharePage) was referencing styles from App.xaml and they weren't compiling. In contemplating your response, @LandLu, I realized that I probably need to start the whole app to use the styles from App.xaml, which is not what I want to do (it should be lightweight). So I copied the few styles I need from App.xaml's resource dictionary to my SharePage and now things are working.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Do you develop a Xamarin Forms project?
    If so, may I ask why you want to load Forms page from the native side?
    We could present this content page directly in Forms like:

    Navigation.PushModalAsync(new SharePage());
    

    If you are using Xamarin iOS directly, there's no App.xaml in the project. We could load the page directly.
    Have you tried this sample:
    https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/native2forms/

  • ntc4ntc4 Member ✭✭
    Accepted Answer

    To clarify, I created a Xamarin Forms app. One of the requirements of the app was to share files from other apps.

    On Android, I created a share activity with an intent filter to handle sharing. On iOS, I created a share extension.

    I was creating a support fragment out of a xamarin forms content page on Android as the UI for sharing. On iOS I was creating a view controller out of the same content page like here https://docs.microsoft.com/en-us/xamarin/ios/platform/extensions-with-xamarinforms.

    The issues was the content page (SharePage) was referencing styles from App.xaml and they weren't compiling. In contemplating your response, @LandLu, I realized that I probably need to start the whole app to use the styles from App.xaml, which is not what I want to do (it should be lightweight). So I copied the few styles I need from App.xaml's resource dictionary to my SharePage and now things are working.

Sign In or Register to comment.