Getting specific data from an object C# - c#

I'm busy with a UWP app that uses a gridview as a category page. I've put a OnClick listener for when one of the the grid items is clicked, and it works. The result is an object which contains the imageURL and imageText.
This is where the problem starts, I need to access the object and retrieve the imagetext, I've tried converting it using various methods but nothing seems to want to work.
Here is the output of the clicked item, which is correct.
This is currently my latest attempt, which simply passes "an object of this type cannot be converted"
private void CategoryItem_Click(object sender, ItemClickEventArgs e)
{
object output = e.ClickedItem;
//string[] arr = ((IEnumerable)e.ClickedItem).Cast<object>()
//.Select(x => x.ToString())
//.ToArray();
}

You can use the as operator to cast the object to a Wiin.MainPage.MyImage:
private void CategoryItem_Click(object sender, ItemClickEventArgs e)
{
MyImage output = e.ClickedItem as MyImage;
if (output == null) return; // the clicked item was not of the expected type
string text = output.ImageText;
string url = output.ImageUrl;
}
Your debugger shows that the object in e.ClickedItem is a MyImage instance, so the cast should work.
In case the MyImage type is a value type (a struct), the as operator won't work. You'd need to cast directly then:
MyImage output = (MyImage)e.ClickedItem;
But this cast will throw an InvalidCastException if e.ClickedItem contains an instance of a different type. You might check the type before casting using is:
if (!e.ClickedItem is MyImage) return;

Related

How to obtain value from an object (c#/xamarin)

My Code
Hello. I am using xamaring forms to create an app and I am having problems obtaining a certain value from an object.
Essentially I would like to take Address1 from the object e.SelectedItem and place it into a string called address. I plan to do this to all the variables in the object, so address, city, country, postal code, etc...
From here I will use these strings to form a url link that will take the user to their native map application with the address inputed into the url.
Note: The line of text I have var item = e.SelectedItem; was added for testing purposes.
Below I have some code:
async void AddressItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem != null)
{
var item = e.SelectedItem;
}
}
In summary: I would like to take the variables stored in an object and place them into their own string element.
Thank you!
you need to cast the object to the correct type first
var item = (MyClass)e.SelectedItem;
var addr1 = item.Address1
my suggestion would be cast your item NOT as "var", cast it as your exact class type.
Class item = (Class)e.selectedItem

Why can't I directly access the properties of a custom object that I've assigned to a WinForms Control.Tag property?

I want to set a Winforms Control.Tag property with my custom object ButtonMetaData and then access the properties of ButtonMetaData using the Control.Tag property. Should work because the Tag property is defined as an object, right? See Figure 1.
However, in order to access the properties of ButtonMetaData, I'm forced to assign the Tag object to an intermediate object variable (x in my example) in order to access the ButtonMetaData properties. When I try and access them using the Tag object that has been cast to ButtonMetaData, the compiler complains. See Figure 2.
Why can't I directly access the properties of ButtonMetaData using the Tag object that has been cast to ButtonMetaData?
Figure 1
Figure 2
public class ButtonMetaData
{
public bool clickedByUser;
public bool clickedProgramatically;
public ButtonMetaData(bool clickedByUser, bool clickedProgramatically)
{
this.clickedByUser = clickedByUser;
this.clickedProgramatically = clickedProgramatically;
}
}
private void Button1_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Tag = new ButtonMetaData(clickedByUser: true, clickedProgramatically: false);
//BUILDS OK
ButtonMetaData x = (ButtonMetaData)button.Tag;
Console.WriteLine(x.clickedByUser);
Console.WriteLine(x.clickedProgramatically);
//BUILDS OK
//DOESN'T BUILD - error on field clickedByUser and error on field clickedProgramatically
Console.WriteLine((ButtonMetaData)button.Tag.clickedByUser);
Console.WriteLine((ButtonMetaData)button.Tag.clickedProgramatically);
//DOESN'T BUILD - error on field clickedByUser and error on field clickedProgramatically
}
You forgot some brackets. You need to cast button.Tag to ButtonMetaData. Try this:
Console.WriteLine(((ButtonMetaData)button.Tag).clickedByUser);
Without brackets you are casting button.Tag.clickedByUser to ButtonMetaData...

Trying to send two stuff inside my pin to the next page, error: Cannot convert method group expression to object

So I want to send two things in my pin when I push it to another page, the "Setbinding" works and the string (myString) gets pushed fine to the other page with the correct info. When I try to push my second string (mySecondString) through a setvalue/getvalue function i get the error: "Cannot convert method group expression to object." I have tried to use a string as well on the "Detailpage" but same error.
pin.SetBinding (Label.TextProperty, new Binding (myString));
pin.BindingContext = myString;
pin.SetValue (Label.TextProperty, mySecondString);
pin.GetValue (Label.TextProperty);
pin.Clicked += onButtonClicked1;
void onButtonClicked1 (object sender, EventArgs e)
{
Pin pin = (Pin)sender;
Navigation.PushAsync (new DetailPage (pin.SetValue, pin.BindingContext ));
}
public DetailPage (object info1, object info2)
Updated version:
pin.SetValue (Label.TextProperty, mySecondString);
new DetailPage (pin.GetValue(Label.TextProperty));
pin.SetValue is a method, so you need to use it as one (by adding brackets).
Also, I assume what you want to use is the getter, not the setter, as SetValue propably doesn't have a return value.
Try:
new DetailPage(pin.GetValue(), pin.BindingContext)

How to access Properties of Objects supplied to event handler? (Can view when debugging, but not with Intellisense.)

I am trying to find which item is selected in a ListView. When the selection is changed, I run the following code:
private void Change_CurrentConnection(object sender, SelectionChangedEventArgs e)
{
var d = e.AddedItems[0];
}
And you can see here what d is:
My question is, how can I access the Id, Name, and Url properties? (The properties are strings from a custom class. The ListView is bound to a collection of objects generated from that class.)
You need to cast the item as your class, which appears to be called Connection? You should first check to see if the object you are casting is actually of the type you wish to cast it to:
if (e.AddedItems[0] is Connection)
{
Connection toAccess = e.AddedItems[0] as Connection;
// Here you can access the properties directly
string myUrl = toAccess.Url;
}
This way we avoid an InvalidCastException.
you can do this as the following also
Connection lstViewItem = (Connection)YourListView.SelectedItems[0];

Extracting property value of image object

I have an image that I want to add a dropshadow effect dynamically when ever the mouse is over the image.
`<Image Name="image1" Stretch="Fill" Source="/Start;component/Images/100px-The_alliance_logo.jpg" MouseEnter="MouseOver" />`
I want to extract the value of Name whenever mouse is hovered over the image. I have coded the following in C#.
private void MouseOver(object sender, MouseEventArgs e)
{
object ObjectName = new object();
ObjectName = Convert.ToString(sender.GetType().GetProperty("Name").GetValue(sender, null));
String Obj = (String)ObjectName;
Obj.Effect = shade(Obj);
}
I am getting an error in Obj.Effect = shade(Obj);, that says : "'string' does not contain a definition for 'Effect' and no extension method 'Effect' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)"
How can I solve it ?
If you insist on doing code-behind, which is not how WPF is supposed to be programmed, then you will need to do this:
private void MouseOver(object sender, MouseEventArgs e)
{
var image = sender as Image;
if(image != null)
{
image.Effect = yourShadowEffect;
}
}
However, there is a good example of how to do it the WPF way here, I strongly suggest you take a look, because the code behind approach will lead you nowhere.
Edit:
In this example, yourShadowEffect is supposed to be a variable of type System.Windows.Media.Effects.Effect, in your case a DropShadowEffect. You can create a new one by calling the constructor:
yourShadowEffect = new DropShadowEffect();
You should not do this in your MouseEventHandler, but rather have this as a class variable, to save resources and not create a new one every time the user hovers it's mouse over the image.

Categories

Resources