簡體   English   中英

ArgumentException:值不在預期范圍內

[英]ArgumentException: Value does not fall within the expected range

在我的XAML頁面中,我添加了一些資源

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

在我的代碼文件中,我正在調用此資源,但遇到運行時異常。 不能理解會發生什么。 請指教

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"
}

資源中不能有FrameworkElement(控件)。 控件只能是另一個元素的子元素,也就是說,您不能重用它,只需將其放在一個位置即可。 而且,如果您將其作為資源放置,則它似乎成為放置它的控件的子級。

如果要重用某些控件,請制作一個UserControlCustom / Template控件,然后改用它。 這是兩種控件之間的比較: 鏈接

這些鏈接可能不是完美的教程,但我認為它們將是一個很好的開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM