Loading Texture2D with String - c#

I'm using XNA and attempting to load an image using a string
image = Game.Content.Load<Texture2D>(playerCharacter.image);
the character class is abstract, the PlayerCharacter classes are all derived from it and the image variable is set to something like "PlayerSprites/Char1"
I get a null exception when running this code. The path is correct, but I don't know if a path is the correct way to do this.

Don't use the file path. Use the asset name. Click on the asset in the solution explorer. You'll see a bunch of properties pop up. Look at the one that says "Asset Name". That is literally the string you pass in to the loadcontent method. Hope I helped!

Related

How to specify a view component under a sub folder within shared/components

I get an error anytime I try to specify a view component from a razor page which lies anywhere but directly under the folder structure of shared/components. For example, shared/components/UserCRUD/{mycomponent} will not work. See the pictures below:
where I include the view component. Note that I have also tried this with typeof() and nameof(), to which the intellisense correctly identifies the correct location under the sub folder.
My relevant folder structure:
The namespace I am using. Note that I have also tried naming this with the "ViewComponent" suffix to no end.
And finally the exception. The most strange thing is that the exception isn't looking at the path I handed it. It's completely omitting UserCRUD.
The folder setup you have doesn't match with the default view location formats.
Even adjusting those doesn't seem to work - or at least I don't succeed in it.
No combination of fixed values and predefined placeholders ({0} view name, {1} page name) seem to give the expected result; there's something with that Components segment that always get added automatically.
Below does work.
In AdminDashboard/Index.cshtml include that UserTable component as below.
You can also keep your current call since that one seems to work.
#await Component.InvokeAsync("UserTable")
Adjust your UserTable component in UserTable.cs by returning the full path to the corresponding view, being /Areas/Identity/Pages/Shared/Components/UserCRUD/UserTable/UserTable.cshtml.
public class UserTable : ViewComponent
{
public IViewComponentResult Invoke(string module)
=> View("/Areas/Identity/Pages/Shared/Components/UserCRUD/UserTable/UserTable.cshtml");
}
You'll have to do similarly for the other views in that UserCRUD folder.

Find .cs file path from any given type in c#

The attribute [CallerFilePath] seems to do what I need to do:
How to find path to .cs file by its type in C#
...but I would like to be able to avoid having to add this new prop to every type that I want to check:
public string SourceFilePath { get; } = Helper.GetCallerFilePath();
What I have tried:
Adding this method at runtime does not seem to be an option.
Using it in a base class does not seem to work either (it returns the
path of the base class).
My guess is that this is somehow feasible, because an exception is able to give you this kind of info (from any given type).
What I really want to do in my particular case is: I have a set of cs files that can be identified via their implemented interface, and I want to know their location in the file structure of the project.
So in this case I don't need to know the file location of any given type, but it would be my preferred approach if that's possible.

How do I store a resource path?

The program I'm making is pretty hard to explain since it's for a very specific personal use so I'll use an example to make it easy to understand:
I have a class in the project named Person that stores an image and a name of a person.
All images are stored in the project resources.
How do I save the resource path/name so that I can reuse it on the main program (not the Person class) ?
For exemple if I create a new Person object in the main program:
Person p = new Person("Michael", Project.Properties.Resources.image);
As what type of variable do I save the path in the Person class?
public Person(string name, ??? image)
Note that I will need to reuse this image later,
for exemple:
this.imageBox.Image = p.image;
I tried using the Image and Bitmap objects but it just changed the imageBox to be blank (I think it sets the imageBox.Image to null. Also, I'm pretty sure that using Bitmap will copy the Image's data and use more memorey for no reason)
I also tried using Image.FromFile and inserting the path as a string but it didn't work.
You should not be messing around with paths if you mean to use resources. They are embedded into the assembly and can be reused from there.
The easiest way to access them is by using the generated resources class, Project.Properties.Resources.Picture. The type of the variable will be an Image.
public Person(string name, Image picture)
If you want to, you can even access the resource by extracting it by hand from the assembly, but that seems to much for this case as far as I can tell.

How to determine the name of the script that called my cmdlet?

In a powershell cmdlet that I am writing in C#, I need to get the name of the script that has called me.
I have derived my cmdlet class from PSCmdlet; there is a ton of information attached to this class at runtime, but I don't see where I can get the information I am looking for.
Is it possible to get the script name? If so, where does it live?
The automatic variable $MyInvocation should contain the name of the script in the InvocationName property.
Thanks, mjolinor... put me on the right path.
MyInvocation.InvocationName gives you the name of the command that the cmdlet was invoked under, but the calling script name is right close by...
I found what I was looking for here (from within the PSCmdlet-derived class):
var callingScript = MyInvocation.ScriptName;
It contains the full path of the script that called the cmdlet.

Using user controls having his name on a string

i have a problem, the functionality I'm looking for exactly is:
I have a grid and datagrid, according to the line to select the datagrid there will be to introduce a user control or other user controls are different pictures I've made polylinesegments, bezier cuadratic ... to introduce the call will name, which build on a string, but I have no way to call it correctly.
This is what I do and it works by putting the full name:
d48.Children.Add(new tratsPintados.end148());
But put the string, tells me not find the path in the project, what I want is to find the path inside the string.
d48.Children.Add(new thestring());
Any ideas?
If you need to instantiate some class based on its name (without real reference), you will need to use Reflection.
Maybe you can do some lookup by name for the class you need, and then use Activator.CreateInstance to call its default constructor.
I hope this is what you want, the question text is quite confusing to me.
using System.Reflection;
public object GetObjectFromString()
{
string objectName = "WpfApplication1.uc1";
Type newType = Type.GetType(objectName, true, true);
object o = Activator.CreateInstance(newType);
// do what you want with the 'o' variable, maybe cast it to the type you want.
}

Categories