2016-10-28 63 views
0

我正在向Xamarin製作的iOS應用添加「Today」擴展部件。我下面這個演練:iOS Widget擴展初始UIViewController

https://developer.xamarin.com/guides/ios/platform_features/introduction_to_extensions/

窗口小部件出現在模擬器我通知部分,但我不能讓任何內容出現在它。它甚至不會創建我創建的UIViewController類,並將其設置爲初始控制器(我知道,因爲它從來沒有在構造函數中碰到我的斷點)。我將它設置爲用此鍵主體類作爲演練解釋說:

enter image description here

任何想法,爲什麼?我也得到這個消息時,我加入了擴展後首次啓動該應用程序:

appname may slow down your phone the developer of this app needs to update it to improve its compatibility

我做了一個樣本項目,Xamarin,並在模擬器上部署時,小部件會出現在這個項目中,只是不與我想在CodeViewController類中添加內容:

https://drive.google.com/file/d/0B8xKHTqtwfKtY0xZN0xaejhlZmM/view?usp=sharing

+0

如果您共享您的示例項目的代碼,它可能會更快重建問題,並盡力幫助你 –

+0

好主意。我添加了一個小樣本項目。 – Darius

+0

它仍然不起作用,但到目前爲止我發現了兩件事。 1.我不知道你是如何創建擴展項目的,但它不是一個exe文件,而是它應該是圖書館。這是錯誤的。我應該可以在項目屬性中看到iOS擴展標籤,但我沒有。 2.應用程序容器必須包含對您的解決方案中沒有發生的擴展的引用。我會繼續挖掘,並讓你知道如果發現別的東西。請讓我知道你是否早點解決它 –

回答

0

爲了節省你2個天,我就可以在這裏度過的是解決方案。

  1. 不要在模擬器上運行它。它不起作用(至少在我的)。
  2. 不要試圖在VS中命中斷點。當您測試擴展程序時,您的應用程序處於後臺模式。 VS不會讓你在調試器中停下來。要證明運行你的任何應用程序,請按回家並嘗試在VS中設置斷點。 VS會掛起,直到你把你的應用程序放到前臺。
  3. 請勿在DidLoad中使用View.Frame。框架的大小是整個屏幕的大小,所以當您將標籤置於中間時,您將看不到它。使用WillAppear這樣

    public override void ViewWillAppear(bool animated) 
    { 
        base.ViewWillAppear(animated); 
    
        if (TodayMessage == null) 
        { 
         // Add label to view 
         TodayMessage = new UILabel(new CGRect(0, 0, View.Frame.Width, View.Frame.Height)) 
         { 
          TextAlignment = UITextAlignment.Center, 
          BackgroundColor = UIColor.LightGray, 
          TextColor = UIColor.Black 
         }; 
    
         // Calculate the values 
         var dayOfYear = DateTime.Now.DayOfYear; 
         var leapYearExtra = DateTime.IsLeapYear(DateTime.Now.Year) ? 1 : 0; 
         var daysRemaining = 365 + leapYearExtra - dayOfYear; 
    
         // Display the message 
         if (daysRemaining == 1) 
         { 
          TodayMessage.Text = String.Format("Today is day {0}. There is one day remaining in the year.", dayOfYear); 
         } 
         else 
         { 
          TodayMessage.Text = String.Format("Today is day {0}. There are {1} days remaining in the year.", dayOfYear, daysRemaining); 
         } 
    
         View.AddSubview(TodayMessage); 
        } 
    } 
    

enter image description here