gotqn
gotqn

Reputation: 43666

Unable to cast object of type 'Windows.UI.Xaml.Controls.Canvas' to type 'Canvas'

I have a canvas and want to get its name when it is Tapped. So, I have this XAML:

<Canvas Name="drawLine" ... >
...
</Canvas>

and this function bind on Tapped event:

private void ChangePage(object sender, TappedRoutedEventArgs e)
{
     string name = ((Canvas)sender).Name.ToString();
     Frame.Navigate(typeof(Params), name);
}

bug get the following error:

An exception of type 'System.InvalidCastException' occurred in gotqn.exe but was not handled in user code

Additional information: Unable to cast object of type 'Windows.UI.Xaml.Controls.Canvas' to type 'gotqn.Canvas'.

Why ((Canvas)sender).Name.ToString(); is generating such error? Is there other way to get the name?

Upvotes: 1

Views: 793

Answers (1)

codingbiz
codingbiz

Reputation: 26396

It seems you have two Canvas classes in different namespaces. I guess the Windows.UI.Xaml.Controls namespace is not in your using so the code is assuming gotqn.Canvas. Did you declare your own Canvas class? If yes, you may have to specify the full name of the Canvas class you want

string name = ((Windows.UI.Xaml.Controls.Canvas)sender).Name.ToString();

Upvotes: 5

Related Questions