How to use a svg file as icon in a .net maui class library?

AbsolutelyFreeWeb 1 Reputation point
2024-11-20T07:15:31.08+00:00

I'm moving some code to a sub project, for better modularity. As I don't want to reference the main app, I want to import the resource to this sub project.

Here is the code I need help with:

 var notificationBuilder = new NotificationCompat.Builder(this, MyActivity.Channel_ID)

.SetContentTitle(title)   
.SetSmallIcon(Resource.mipmap.appicon)
.SetContentText(messageBody)
.SetChannelId(MyActivity.Channel_ID)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true)
.SetPriority((int)NotificationPriority.Max);

I copied the svg file to a new resource folder in the sub project. I have tried build action embedded resource and MauiIcon. But I don't get a resource to be made. I've tried adding properties/Resources.resx, I fill in the values and choose the file, but nothing gets populated in the list. perhaps it only wants .ico files and not .svg.

I've manually added this to my project file:

<ItemGroup>
		<MauiIcon Include="Resources\AppIcon\appicon2.svg">
		  <Color>#512BD4</Color>
		  <ForegroundFile>Resources\AppIcon\appiconfg2.svg</ForegroundFile>
		</MauiIcon>
</ItemGroup>

But in the code, I don't know how to refer to the icon. It does not populate after Resource. What else should I try to be able to setSmallIcon ? It does not understand mipmap or AppIcon

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,951 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,657 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,058 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.