1
votes

In my XAML page I have added some resource

<Page.Resources>
    <Button x:Key="btn" Content="Test Button"></Button>
    ...
</Page.Resources>

In my code file I am calling this resource but getting runtime exception. Cant understand what happens. Please advice

Button btn = this.Resources["btn"] as Button;
if (btn != null)
{
    MyPivotItem.Content = btn; // here I am getting the exception 
                               // "Value does not fall within the expected range"
}
1
MyPivotItem.Content <-- is this a button? - Christian Phillips
MyPivotItem is a PivotItem, I am adding btn as Content - Kalyan
Could you be missing a namespace declaration? - BanksySan

1 Answers

1
votes

You cannot have a FrameworkElement (a Control) in Resources. A control can only be a child of one other element, that is you cannot reuse it, just put it in one place and that's it. And if you put it as resource, it seems that it becomes child of the control in which you put it.

If you want to reuse some control, make a UserControl or Custom/Template Control and use it instead. Here's a comparison between the two types of controls: link

The links may not be the perfect tutorials, but I think they'll be a fine start.