iOS 11上的Xamarin Today小部件几乎所有时间都无法显示

时间:2017-10-09 10:33:38

标签: ios xamarin xamarin.ios ios11 today-extension

我有今天的小部件,有时候"显示更多"不管用。此问题仅出现在iOS 11中,之前的版本(8,9,10)运行良好。我不明白我做错了什么以及为什么我不能扩展小部件。我已经调查了这个问题并发现了" WidgetPerformUpdate"改变后没有触发" PreferredContentSize"内部" WidgetActiveDisplayModeDidChange"这是因为" View.Bounds"更改" PreferredContentSize"。

后,小部件未更改

以下是我的代码示例:

[Register ("TodayViewController")]
public class TodayViewController : UIViewController, INCWidgetProviding
{
    private const int PrefferedHeight = 300;

    private readonly ISettings _settings;

    public TodayViewController(IntPtr handle) : base (handle)
    {
        _settings = AppContainer.Resolve<ISettings>();
    }

    static TodayViewController()
    {
        AppContainer.Container = (new iOSAppSetup()).CreateContainer();
    }

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

        AddOpenApplicationEvent();

        // Tell widget it can be expanded
                ExtensionContext.SetWidgetLargestAvailableDisplayMode(NCWidgetDisplayMode.Expanded);

        // Get the maximum size
        var maxSize = ExtensionContext.GetWidgetMaximumSize(NCWidgetDisplayMode.Expanded);
        PreferredContentSize = new CGSize(maxSize.Width, PrefferedHeight);

        View.AddSubview(***view according to maxSize***);
    }

    private void AddOpenApplicationEvent()
    {
        var tapWidget = new UITapGestureRecognizer();
        tapWidget.AddTarget(() =>
        {
            UIApplication.SharedApplication.OpenUrl(new NSUrl("test://"));
        });

        View.AddGestureRecognizer(tapWidget);
    }

    [Export("widgetPerformUpdateWithCompletionHandler:")]
    public void WidgetPerformUpdate(Action<NCUpdateResult> completionHandler)
    {
        UpdateWidget();

        switch (ExtensionContext.GetWidgetActiveDisplayMode())
        {
            case NCWidgetDisplayMode.Compact:
                // changes according to compact mode
                break;
            case NCWidgetDisplayMode.Expanded:
                // changes according to expanded mode
                break;
        }

        completionHandler(NCUpdateResult.NewData);
    }

    [Export("widgetActiveDisplayModeDidChange:withMaximumSize:")]
    public void WidgetActiveDisplayModeDidChange(NCWidgetDisplayMode activeDisplayMode, CGSize maxSize)
    {
        // Take action based on the display mode
        switch (activeDisplayMode)
        {
            case NCWidgetDisplayMode.Compact:
                PreferredContentSize = maxSize;
                // changes according to compact mode
                break;
            case NCWidgetDisplayMode.Expanded:
                PreferredContentSize = new CGSize(maxSize.Width, PrefferedHeight);
                // changes according to expanded mode
                break;
        }
    }

    private void UpdateWidget()
    {
        // some updates
    }

P.S。有时它通过小部件导航到应用程序后工作。

1 个答案:

答案 0 :(得分:0)

iOS-11 中, Show More/Show Less 工作正常。

我已经创建了一个基本样本来描述这个:

class TodayViewController: UIViewController, NCWidgetProviding
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    }

    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void))
    {
        completionHandler(NCUpdateResult.newData)
    }

    func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize)
    {
        if activeDisplayMode == .expanded
        {
            preferredContentSize = CGSize(width: 0.0, height: 300.0)
        }
        else
        {
            preferredContentSize = maxSize
        }
    }
}