Xamarin-iOS今日小部件,无法加载问题

时间:2017-12-22 10:09:45

标签: c# ios xamarin xamarin.ios

我正在制作今天的小部件扩展程序,在那里我获得了硬币符号,然后我发出了Web请求来获取对象。

在每次重新加载时,小部件再次绘制视图并调用WidgetPerformUpdate方法,从1/10小部件显示我无法加载。

当我将一些对象添加到窗口小部件中时,我对应用程序进行了调整,问题就出现了。

我已经在这个问题上堆了大约一个星期,我已经阅读了所有今天的小部件文档,教程没有任何帮助。

我不知道该怎么做,无法加载是我的噩梦。

有我的TodawyWidgetViewController代码。

using System;
using System.Collections.Generic;
using NotificationCenter;
using Foundation;
using UIKit;
using CryptoCurrencyPCL.POCO;
using CryptoCurrencyPCL.Middleware;
using System.Linq;
using System.Threading.Tasks;
using CryptoCurrencyPCL.Extensions;
using CryptoCurrencyPCL.Enums;
using CoreGraphics;
using Newtonsoft;
using Newtonsoft.Json;
using CryptoPCL.POCO;

namespace CryptoTodayWidget
{
    public partial class TodayViewController : UIViewController, INCWidgetProviding,IUITableViewDataSource,IUITableViewDelegate
    {
        NSUserDefaults userDefaults;
        const string ReuseId = "currencyCellReuseId";
        List<CoinDetail> _coins;
        List<CoinDetail> _cachedCoins;
        List<FavoriteCoin> _favorites;
        List<string> listOfStrings = new List<string>();
        Currency itemCurrency;
        private CGSize _maxSize;
        private string nsStringForValues;
        private string nsString;
        private List<string> deserializedObjectsSymbols;
        bool _firstInit = true;


        protected TodayViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell(ReuseId, indexPath) as WidgetCell;

            var item = _coins[indexPath.Row];
            var favorite = _favorites[indexPath.Row];

            cell.InitData(item,favorite,itemCurrency);

            return cell;
        }

        public nint RowsInSection(UITableView tableView, nint section)
        {
            return _coins?.Count ?? 0;
        }

        [Export("tableView:heightForRowAtIndexPath:")]
        public nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return 50;
        }

        [Export("numberOfSectionsInTableView:")]
        public nint NumberOfSections(UITableView tableView)
        {
            return 1;
        }

        public async override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var webClient = CryptoCurrencyPCL.Services.CryptoWebClient.Instance;
            tableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;

            _cachedCoins = _coins;
            if(_coins != null) {
                initTableView();
            }

            ExtensionContext.SetWidgetLargestAvailableDisplayMode(NCWidgetDisplayMode.Expanded);
        }

        private void initTableView()
        {
            tableView.DataSource = this;
            tableView.Delegate = this;
            tableView.AllowsSelection = false;
            tableView.ReloadData();
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

        }

        [Export("widgetPerformUpdateWithCompletionHandler:")]
        public async void WidgetPerformUpdate(Action<NCUpdateResult> completionHandler)
        {
            var webClient = CryptoCurrencyPCL.Services.CryptoWebClient.Instance;



            try
            {
                initLoading();
                bool check = false;
                userDefaults = new NSUserDefaults("group.com.mpdc.todayextension", NSUserDefaultsType.SuiteName);
                nsStringForValues = userDefaults?.StringForKey(new NSString("MyAppsValues"));
                nsString = userDefaults?.StringForKey("currencyKey");
                itemCurrency = (Currency)Enum.Parse(typeof(Currency), nsString);

                _favorites = JsonConvert.DeserializeObject<List<FavoriteCoin>>(nsStringForValues);

                deserializedObjectsSymbols = _favorites.Select(o => o.FavoriteCoinSymbol).ToList();


                _coins = await webClient.GetMultiCoinDetailsAsync(deserializedObjectsSymbols, itemCurrency); 


                if (ExtensionContext.GetWidgetLargestAvailableDisplayMode() == NCWidgetDisplayMode.Compact)
                {

                    this.PreferredContentSize =_maxSize;
                }

                else
                {
                    this.PreferredContentSize = new CoreGraphics.CGSize(0, _coins.Count * 50);
                }

                initTableView();
                initLoading(false);

                tableView.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine;

                completionHandler(NCUpdateResult.NewData);

            }
            catch (Exception ex)
            {
                initLoading(false);
                completionHandler(NCUpdateResult.Failed);
                Console.WriteLine(ex);
            }

        }


        void initLoading(bool visible = true)
        {

            if (visible)
            {
                ActivityIndicator.Hidden = false;
                ActivityIndicator.StartAnimating();
                return;
            }

            ActivityIndicator.Hidden = true;
            ActivityIndicator.StopAnimating();
        }

        [Export("widgetActiveDisplayModeDidChange:withMaximumSize:")]
        public void WidgetActiveDisplayModeDidChange(NCWidgetDisplayMode activeDisplayMode, CoreGraphics.CGSize maxSize)
        {
            _maxSize = maxSize;

            if (activeDisplayMode == NCWidgetDisplayMode.Compact)
            {

                this.PreferredContentSize = _maxSize;
            }

            else
            {
                if(_coins!=null)
                this.PreferredContentSize = new CoreGraphics.CGSize(0, _coins.Count * 50);
            }
        }

    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

我解决了。

它正在模拟器上工作,但不在设备上。

当我在调试模式下在设备上运行我的应用程序时,问题就出现了。

我认为在调试模式下,会出现内存问题。

我在发布模式下运行它,一切正常。