The type or namespace name 'Platform' does not exist in the namespace 'Microsoft.Maui.Graphics' (are you missing an assembly reference?)

Randy Clegg 45 Reputation points
2023-03-16T20:44:11.47+00:00

I am using Visual Studio Community 2022 64 bit 4.8.04161 on Windows 11 Pro.

I need to be able to draw an image at a particular location. I am attempting to use the documentation at https://learn.microsoft.com/en-us/dotnet/maui/user-interface/graphics/images?view=net-maui-7.0 or https://learn.microsoft.com/en-us/dotnet/maui/user-interface/graphics/images?view=net-maui-6.0 for displaying an IImage

I have created a default .net maui project using visual studio using both 7.0 and 6.0 and then tacked the example code into MainPage::OnCounterClicked. This isn't what I want, of course, but I'm just trying to figure out how to get a clean build.

I have seen the note that PlatformImage is not supported on windows, but not matter what platform I build for, Andriod, iOS, or Windows, I get the error in the title.

Here are the complete errors as reported by the build:

Severity Code Description Project File Line Suppression State

Error CS0234 The type or namespace name 'Platform' does not exist in the namespace 'Microsoft.Maui.Graphics' (are you missing an assembly reference?) GraphicsTest6 (net6.0-windows10.0.19041.0) C:\CleverEggSoftware\trunk\projects\GraphicsTest6\GraphicsTest6\MainPage.xaml.cs 1 Active

Severity Code Description Project File Line Suppression State

Error CS0103 The name 'PlatformImage' does not exist in the current context GraphicsTest6 (net6.0-windows10.0.19041.0) C:\CleverEggSoftware\trunk\projects\GraphicsTest6\GraphicsTest6\MainPage.xaml.cs 31 Active

Here is the hacked class

using Microsoft.Maui.Graphics.Platform;
using System.Reflection;

namespace GraphicsTest6;

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
	}

	private void OnCounterClicked(object sender, EventArgs e)
	{
		count++;

		if (count == 1)
			CounterBtn.Text = $"Clicked {count} time";
		else
			CounterBtn.Text = $"Clicked {count} times";

		SemanticScreenReader.Announce(CounterBtn.Text);

        Microsoft.Maui.Graphics.IImage image;
        Assembly assembly = GetType().GetTypeInfo().Assembly;
        using (Stream stream = assembly.GetManifestResourceStream(
            "GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
        {
            image = PlatformImage.FromStream(stream);
        }
        ICanvas canvas = null;
        if (image != null) {
            canvas.DrawImage(image, 10, 10, image.Width, image.Height);
        }
    }
}

What am I missing or doing wrong?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,866 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,491 Reputation points Microsoft Vendor
    2023-03-28T02:13:35.08+00:00

    Hi Randy Clegg,

    but not matter what platform I build for, Andriod, iOS, or Windows, I get the error in the title.

    If you wrote code in the shared project, MAUI would build for all platform by default, if you want to build in the specific platform. Please open your project folder. Find the GraphicsTest6.csproj and open it. This error appears on the windows platform. If you want to test in the Android, iOS platforms. you need to remove <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks> in the <PropertyGroup> tag, then rebuild your project for Android,iOS and maccatalyst platform. Is this error disappeared?

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,491 Reputation points Microsoft Vendor
    2023-03-17T06:05:01.09+00:00

    Hello,

    You can wrap the namespace and execute code with Conditional compilation For example, if this plaform do not support windows, you can use Conditional compilation to wrap the namespace.

    #if ANDROID|| IOS || MACCATALYST
    using Microsoft.Maui.Graphics.Platform;
    #endif
    

    And wrap the execute code. These build errors will disappear.

    #if ANDROID|| IOS || MACCATALYST
            Microsoft.Maui.Graphics.IImage image;
            Assembly assembly = GetType().GetTypeInfo().Assembly;
            using (Stream stream = assembly.GetManifestResourceStream(
                "GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
            {
                image = PlatformImage.FromStream(stream);
            }
            ICanvas canvas = null;
            if (image != null)
            {
                canvas.DrawImage(image, 10, 10, image.Width, image.Height);
            }
    #endif
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments