我試圖創建一個新實例(的Expander
),並在每次變量不等於另一個變量時將其放入StackPanel
。在if語句中創建對象的實例
問題;但是,似乎是我無法訪問if函數外的變量。 我得到:
"Error The name 'expand' does not exist in the current context"
如果我聲明,那麼應用程序的工作原理通過if語句預期運行一次函數之外的新實例。它通過if語句運行第二次,我得到一個錯誤:
"Specified Visual is already a child of another Visual or the root of a CompositionTarget."
if (lastStatement != first.ToString()) {
i++;
Expander expand = new Expander();
expand.Name = "expander" + i.ToString();
stackpanel1.Children.Add(expand); //This where the error is
} expand.Content += ones; // I need to place this here for the code to work
這是我得到的情況:
"Error The name 'expand' does not exist in the current context"
第二次嘗試(當我有外面創建實例聲明)我得到:
"Specified Visual is already a child of another Visual or the root of a CompositionTarget."
感謝PSWG,它的工作。我所做的只是聲明擴展器在函數外部爲空,並在本地調用它。 – xterminal0 2013-04-27 04:07:55