簡體   English   中英

如何檢查UIElement是否為全景圖

[英]How to check if an UIElement is a Panorama or not

我有這樣的代碼

        foreach(UIElement iue in layout.Children)
        {
            if (iue is Panorama)
            {
                //some code
            }
        }

但這總是錯誤的。

我也試過

        foreach(UIElement iue in layout.Children)
        {
            if (iue.getType() == typeof(Panorama))
            {
                //some code
            }
        }

沒有成功。

我編寫了一個測試應用,您的代碼應該可以正常工作。 如果您仔細檢查了其他所有內容,請記住, Children屬性將僅返回頂級元素。 如果您的Panorama控件是子元素,則需要使用父控件的Children屬性來訪問它。 要檢查的另一件事是,您在typeof(Panorama)行中使用的控件的命名空間是Microsoft.Phone.Controls 您可以通過在光標位於Panorama單詞上單擊F12來完成此操作。

只需對您的代碼進行一點更改。 試試這個,可能會幫助您。

foreach(UIElement iue in layout.Children)
        {
            if (this.IsPanorama(iue ))
            {
                //some code
                //Panorama control
            }
            else
             {
               //not aPanorama control
             }
        }

//只檢查UI元素是否為全景圖

private bool IsPanorama(UIElement element)
{
    bool isPanorama =false;
   try{
       Panorama p = (Panorama)element;
        isPanorama = true;
       return isPanorama ;
      }
      catch(Exception ex)
      {
        isPanorama = false;
        return isPanorama ;
      }
}

只需再次檢查XAML,發現沒有Panorama,而是一個從Panorama繼承的名稱為PanoramaFullScreen(具有全屏PanoramaItem)的UserControl。

謝謝大家的回應。

暫無
暫無

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

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