Xamarin Android(Mac) persisting error: "Could not load System.Drawing" - c#

First let make clear that this is my first Android project ever that I'm trying to complete, so I'm still very new to all this.
I've been stuck with this extremely annoying problem where I can't compile my project anymore even after removing a lot things. At one point in my project I added the 'System' namespace to use the 'Exception' keyword(to test a DB connection). All was working fine and well right before this point. But after trying to compile I got the error:
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: error : Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'System.Drawing.dll'
So I just added 'using System.Drawing' on top but later I read that Xamarin Android doesn't support that. So I removed it again and tried to remove whatever I did to try stop getting this Exception. Then this error got stuck forever even if I comment all things out. I can write 'using System.Drawing' without the system giving that line an error, which I thought was weird because it's nowhere to found in the references. I also really don't get it since I don't draw anything and don't think use anything from System.Drawing. Any time I reference the 'System' package I get this error, I can't go without because some android files rely on it. I've now lost hours now without any progress and basically am at the end of my road here.
Here's my MainActivity.cs file as it is now, narrowed down, it's my only .cs file:
using Android.Graphics;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using MySql.Data.MySqlClient;
using System;
namespace MapApp
{
[Activity(Label = "MapApp", MainLauncher = true, Icon = "#mipmap/icon")]
public class MainActivity : Activity, IOnMapReadyCallback
{
GoogleMap GMap;
/// database
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
}
private void SetUpMap()
{
if (GMap == null)
{
FragmentManager.FindFragmentById<MapFragment>(Resource.Id.googlemap).GetMapAsync(this);
}
}
public void OnMapReady(GoogleMap map)
{
GMap = map;
GMap.SetLatLngBoundsForCameraTarget(new LatLngBounds(
new LatLng(51.873176, 4.393930),
new LatLng(51.994576, 4.598036)));
GMap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(51.917879,4.481134),13));
/*MarkerOptions markerOpt1 = new MarkerOptions();
markerOpt1.SetPosition(new LatLng(51.917879, 4.481134));
markerOpt1.SetTitle("Vimy Ridge");
GMap.AddMarker(markerOpt1);*/
CircleOptions dangerZone = new CircleOptions();
dangerZone.InvokeCenter(new LatLng(51.917879, 4.481134));
dangerZone.InvokeRadius(100);
dangerZone.InvokeFillColor(0x7F00FF00);
dangerZone.InvokeStrokeWidth(0);
GMap.AddCircle(dangerZone);
}
}
}
Here is a picture of my references and packages.(a lot of Xamarin packages in there because those were needed to load Google Maps):
Here another picture of my project structure as is, if it may help identify the problem.
I have tried to delete all my packages and clean my Big/Debug folder and rebuild everything but error still proceeds.
Everything worked perfectly and now I just can't build it anymore. I wrote "using System" once, got this error, and then never couldn't get rid of this error anymore.
I really do not want to restart my project for the second time, after getting getting unknown errors every hour so any help would GREATLY be appreciated.

I have found the problem thanks to using Diagnostic logging.
Turns out the 'MySql.Data' package loads 'System.Drawing' which of course is not supported in Xamarin.Android.
Dependency System.Drawing, Version=4.0.0.0, Culture=neutral,
Required by MySql.Data, Version=6.9.9.0, Culture=neutral
Thanks to Lexi Li for giving the one tip I exactly needed!

Related

Build.cs not working even after being restores to it's previous version

I'm trying to compile a project I'm working on and this error mesages pops out:
Invalidating makefile for SpaceShooterEditor (SpaceShooter.Build.cs modified)
While compiling E:\ue projects\SpaceShooter\Intermediate\Build\BuildRules\SpaceShooterModuleRules.dll:
e:\ue projects\SpaceShooter\Source\SpaceShooter\SpaceShooter.Build.cs(3,29) : error CS0246: The type or namespace name 'ModuleRules' could not be found (are you missing a using directive or an assembly reference?)
e:\ue projects\SpaceShooter\Source\SpaceShooter\SpaceShooter.Build.cs(5,25) : error CS0246: The type or namespace name 'ReadOnlyTargetRules' could not be found (are you missing a using directive or an assembly reference?)
ERROR: Unable to compile source files.
Any idea why this might happen? Last night i was trying to code a widget, modified the build.cs, then replaced the modified version (which chrashed the game) with a build.cs that worked previously, and still nothing? Is there any hope to make it work or should i start over? Moreover, how can this be avoided?
I already did the restarts and refreshes. I went to even delete the binaries and some cashed files and it didn't work.
Below you'll find the content of the Build.cs:
public class SpaceShooter : ModuleRules
{
public SpaceShooter(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
When i try to input specify the using UnrealBuildTool;, Visual Studio, for some reason, deletes it when i hit compile or save.
After some digging, i found out that wrapping the content in a namespace UnrealBuidTool.Rules{} solves this. In the end, the build file looks like this:
namespace UnrealBuildTool.Rules
{
public class SpaceShooter : ModuleRules
{
public SpaceShooter(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
}
As you do not share what is included in your Build.cs file I will just explain what the error is and how you could fix it. The error basically says that it couldn't compile the Build.cs file based on the lines 29 and 25, and that the types that are used there called 'ModuleRules' and 'ReadOnlyTargetRules' could not be found in any of the namespaces that were included by the using statements at the top of Build.cs. These types are both stored in the UnrealBuildTool namespace and can thus be included in your file by typing:
using UnrealBuildTool;
This however seems like a trivial solution to your problem, but as you do not share a lot of info this is the best solution I can give you for now.

C# Loading Assemblies With Names Containing a Dot

In my c# application (asp.net mvc), I want to use Autofac to handle my dependency injection, but I got an error when atttempting to use the RegisterAssemblyTypes function (error message below).
I did some experiments with a new solution and found that the error seems to be related to a dot in the project name. The test solution looks like this:
AssemblyTest.sln
- Main.csproj
--> Program.cs
- TestLoadAssembly.csproj
--> Test.cs
- TestWith.Dots.csproj
--> Test.cs
The test classes both have one method that prints "Hello" to the screen. Program.cs looks like this:
using System.Reflection;
namespace Main
{
class Program
{
static void Main(string[] args)
{
var x = Assembly.Load(nameof(TestLoadAssembly)); //This works
var y = Assembly.LoadFrom("TestWith.Dots.dll"); // This seems to work
var z = Assembly.Load(nameof(TestWith.Dots)); // Error here
}
}
}
Main is the active project, and there are project references from Main to the other two. All projects are .net 5 projects.
The error I get from line 3:
System.IO.FileNotFoundException: 'Could not load file or assembly 'Dots, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
My question is, how do I make the third line (z) work? Since the first line works, but the third line does not, it seems the error is related to the dot in the name, or is there something that I'm missing here? However, as line two shows, it seems to work when loading the dll by referencing it directly by name.
I have been staring myself blind at this, so I would appreciate your help!

PUN Networking error - No Suitable Method found to override

I'm fairly new to C# and unity, and i've been trying to set up the Photon Networking service. I've run into an issue i can't figure out the cause of. Here are the two error messages.
Assets\PhotonUnityNetworking\Resources\TestConnect.cs(21,41): error CS0246: The Type or namespace name 'DisconnectCause' could not be found (are you missing a using directive or an assembly reference?)
Asseta\PhotonUnityNetworking\Resources\TestConnect.cs(21,26): error CS0115 'TestConnect.OnDisconnected(DisconnectCause)': no suitable method found to override.
However, in visual studio, nothing shows up as incorrect.
Any help as to the cause of these issues appreciated.
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestConect : MonoBehaviourPunCallbacks
{
private void Start()
{
print("Connecting to server...");
PhotonNetwork.GameVersion = "0.0.1";
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
print("Connected to server!");
}
public override void OnDisconnected(DiconnectionCause cause)
{
print("Disconnected from Server for reason " + cause.ToString());
}
}
First another important note: You should not put any of your custom scripts inside a folder called Resources!
Also afaik PhotonUnityNetworking is their official folder of Photon itself. I'ld recommend to not modify or add anything within it, makes it hard to upgrade or fix it later if you have messed it up somehow.
Then to your issue
It is as the error already suggests
are you missing a using directive
DisconnectCause belongs to another namespace Photon.Realtime.
You will need to add a
using Photon.Realtime;
on the top of that file or have to address it directly like
public override void OnDisconnected(Photon.Realtime.DisconnectCause cause)
{
...
}
Strange though that VisualStudio doesn't note that...
The second error is just a follow-up compile error not finding the according method to override because of a supposed signature mismatch since it doesn't know the type you used as parameter due to the error before.
In the future please don't post screenshots of code and error messages! This makes it not only harder to solve the issue but also makes it impossible for others to find this thread when searching for the error message content in order to see if it was already solved somewhere!
Rather copy & paste the raw text I your question and format it as code using the { } button.

xamarin C#: 'Resource' does not contain a definition for 'Id'

I recently started working with Xamarin to build Android apps in C#.
There is one issue I have had in particular that makes it very hard for me to make any progress:
error CS0117
I have two identical projects, the issue only shows up in one of them.
It originally occured in both but rebuilding a few times fixed the first one.
The second one seems to be more persistent.
I really need to find a solution to this issue, as referencing is very basic and needed. Not to mention, it happens in every new project.
Here is my code:
NoteMath
using System;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
namespace NoteMath_v1._0._1
{
[Activity(Label = "noteMath", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Window.RequestFeature(Android.Views.WindowFeatures.NoTitle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
EditText etInput = FindViewById<EditText>(Resource.Id.etInput);
TextView tvConsole = FindViewById<TextView>(Resource.Id.tvConsole);
etInput.KeyPress += (object sender, EditText.KeyEventArgs e) =>
{
e.Handled = false;
if (e.Event.Action == Android.Views.KeyEventActions.Down && e.KeyCode == Android.Views.Keycode.Enter)
{
tvConsole.Append("\n>" + HandleInput.TransferInput(etInput.Text));
etInput.Text = "";
e.Handled = true;
}
};
}
}
}
The code you posted looks good. The error is clearly not coming from there. You´ve got something wrong in any of your resource layout or xml files.
The problem actually is that Visual Studio won´t show up Resource generation errors in the Errors panel, eventhough it should be like that (I think this will be fixed in future versions). At the moment it is just telling you that the class Id doesn´t exists.
Resources.designer.cs is a class generated by Xamarin when you edit any resource file. It containts references to any declaration, id, etc in the xml/axml files. If the resource has an error, Resources.designer.cs generation will fail, but you get a really vague error hint.
The first thing you can try is rebuilding your Android project, forcing all resources to be generated again. Otherwise try the following:
Good news is that you can see generation errors by changing build output verbosity to "detailed":
Now compile your project again and check the output window. It will tell you exactly what the problem is.
After you fix it, Resource.Id class will be generated.
For me, this worked:
Step 1.
Project > Update NugetPackages
Step 2.
Build > Clean All
Step 3.
Build > Build All
I am super new. The only thing that really worked for me was manually running this powershell script in my project root:
Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }
I am not sure why the live view/build output isn't more helpful, or that visual studio isn't cleaning these files correctly in my xamarin project.
Special thanks to this guy:
https://montemagno.com/easily-clean-bin-obj-folders/

Xamarin assembly conflicts with the imported type

So I was changing my Project name and also my folders etc. I followed guide from here How do I rename a Project Folder from within Visual Studio?. All seemed to work, I can launch my app, but I got some erros and lots of lots of warnings
Warning example:
Warning CS0436 The type 'ObservableObject' in 'C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\Base\ObservableObject.cs' conflicts with the imported type 'ObservableObject' in 'TodoScheduler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\Base\ObservableObject.cs'. Volaapp C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\Base\SelectableObject.cs 5 IntelliSense Active
And errors like this:
Error CS0234 The type or namespace name 'XamlFilePathAttribute' does not exist in the namespace 'Xamarin.Forms.Xaml' (are you missing an assembly reference?) Volaapp C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\obj\Debug\TodoScheduler.Pages.MenuPage.xaml.g.cs 14 IntelliSense Active
The code of the error:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace TodoScheduler.Pages {
[global::Xamarin.Forms.Xaml.XamlFilePathAttribute("C:\\Users\\Godhaze\\Documents\\Visual Studio 2017\\Projects\\Volaapp\\Volaapp\\Volaapp\\Pa" +
"ges\\MenuPage.xaml")]
public partial class MenuPage : global::TodoScheduler.Controls.BasePage {
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private global::Xamarin.Forms.ListView listView;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent() {
global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(MenuPage));
listView = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.ListView>(this, "listView");
}
}
}
So I think Its an problem with Assembly?
I Added Reference In the first place, so what could be the problem?
Also do I need to change my namespaces? My projects name of PCL is "Volaapp" But Im using namespaces like: TodoScheduler.Base, because I think its refers to the Assembly. I might misunderstood this!
I tried cleaning the solution then deleting bin,obj folders and then rebuilding it, but that dident help.
So what should I do? I really want to fix this case and I can even give the full code of this to fix :(
I had the same problem, and found out I was performing a font size change operation on an array of labels that contained an image by accident.

Categories