【问题标题】:Xamarin-iOS today widget, unable to load issueXamarin-iOS 今天小部件,无法加载问题
【发布时间】:2017-12-22 10:09:45
【问题描述】:

我正在制作今天的小部件扩展,我在其中获取硬币符号,然后我正在发出 Web 请求以获取对象。

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

我调试了应用程序,当我将一些对象添加到小部件中时,问题出现了。

我已经解决了这个问题大约 1 周,我已经阅读了今天所有的小部件文档,教程没有任何帮助。

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

这是我的 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);
            }
        }

    }
}

谢谢

【问题讨论】:

  • 您可以发布崩溃代码以查看问题。可能是“CryptoCurrencyPCL”的问题,或者加载数据的时间太长。因为 Today 扩展不应该用于冗长的任务。
  • 很奇怪的问题是它在模拟器上工作,而不是在真实设备上
  • 模拟器和真机有些区别。可能你的数据太多了,试着减少你的datasouce再调试吧。

标签: c# ios xamarin xamarin.ios


【解决方案1】:

我解决了。

它在模拟器上运行,但在设备上不运行。

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

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

我已经在发布模式下运行它并且一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多