简体   繁体   中英

Unable to add image to xcassets in Xamarin.iOS project with Visual Studio 2022

I have recently updated to Visual Studio for Mac 2022 and since then I have been having the following issue: I am unable to add new images to the Media.xcassets folder

On VS for Mac 2019 I was able to achieve this within Visual Studio, now when I double click the xcassets folder in the left panel browser, the file is opened in Xcode. I can add a new image set in Xcode but the change is not reflected when I clean, build & deploy the application from Visual Studio. Any instances of the new image I use on xib files or load programatically eg UIImage.FromBundle("new_image") do not display in the app. Images that were previously added to the project using VS for Mac 2019 display correctly.

This appears to be a bug with VS for Mac 2022, so really I'm asking if anyone is aware of a workaround?

This appears to be caused by a bug in VS 2022. I worked around this problem with the following steps:

  1. Make sure your Media.xcassets folder is in the root directory of your Xamarin iOS project, the same place as your .csproj file eg. MyApp/MyApp.iOS/Media.xcassets - in my case, it was in MyApp/MyApp.iOS/Resources/Media.xcassets so I had to move it

  2. Double click the Media.xcassets file in Visual Studio to open it in Xcode

  3. Add a new image in Xcode and save

  4. When returning to visual studio you should see "Error synchronizing changes from Xcode" in the bottom left of the VS window (the VS bug)

  5. Update your iOS project file (MyApp/MyApp.iOS/MyApp.iOS.csproj) to include references to both the Contents.json and the pdf/png file for your new image. For example I had to add these two lines:

     <ImageAsset Include="Media.xcassets\new_image.imageset\Contents.json" /> <ImageAsset Include="Media.xcassets\new_image.imageset\new_image.pdf" />
  6. When you next build and deploy, the new image should appear in the app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM