【问题标题】:Unable to Bind MvvMCross 6.x iOS color to MvxColor WithConversion无法将 MvvMCross 6.x iOS 颜色绑定到 MvxColor WithConversion
【发布时间】:2018-10-20 21:09:18
【问题描述】:

我是 MvvMCross 的新手,在将我的视图模型 mvx 属性绑定到 iOS 中的背景颜色时遇到问题。我从头开始设置了自己的项目,但为了确认它不是设置中的内容,然后我使用 MvxScaffolding 创建了一个新项目。

我得到的错误是:

 [ERROR] (MvxBind) Problem seen during binding execution for binding
 TintColor for Colour - problem ArgumentException: Object of type 
'MvvmCross.UI.MvxColor' cannot be converted to type 'UIKit.UIColor'.

查看模型:

using MvvmCross.UI;

namespace BindingTests.Core.ViewModels.Main
{
    public class MainViewModel : BaseViewModel
    {

        public MvxColor Colour { get { return MvxColors.AliceBlue; }}
    }
}

查看

using System;
using MvvmCross.Platforms.Ios.Presenters.Attributes;
using MvvmCross.Platforms.Ios.Views;
using BindingTests.Core.ViewModels.Main;
using MvvmCross.Binding.BindingContext;

namespace BindingTests.iOS.Views.Main
{
    [MvxFromStoryboard(nameof(MainViewController))]
    [MvxRootPresentation(WrapInNavigationController = true)]
    public partial class MainViewController : BaseViewController<MainViewModel>
    {
        public MainViewController(IntPtr handle) : base(handle)
        {
            var set = this.CreateBindingSet<MainViewController, MainViewModel>();
            set.Bind(LabelWelcome).For(l => l.BackgroundColor).To(vm => vm.Colour).WithConversion("NativeColor");
            set.Apply();
        }
    }
}

有人有什么想法吗?我难住了。 进阶干杯

忘了说,这适用于 MvvmCross 5.x

【问题讨论】:

  • 您是否在当前项目中安装了MvvmCross.Plugin.Color package
  • 是的,已安装插件。我可以很好地使用 MvxColor。这只是转换。我忘了提,使用 MvvMCross 5.7 效果很好。
  • 您能否尝试将此行添加到 iOS 上的 LinkerPleaseInclude 文件中(具体使用哪种方法并不重要):var converter = new MvxNativeColorValueConverter();
  • @nmilcoff ,工作得很好。非常感谢

标签: c# xamarin xamarin.ios mvvmcross


【解决方案1】:

MvvmCross 6 带有一个注册插件的新机制。

我们遇到了两种问题:

1) 我们在发布之前没有发现的一个问题是,框架现在假定插件程序集将在它们注册时被加载。不幸的是,我们无法保证(除非显式使用程序集,否则运行时通常不会加载程序集)。

2) 链接器以前不是问题,因为框架有引导文件,可确保代码不会被删除。我们在最重要的类上添加了Preserve 属性,但显然这还不够。

在我写这个答案的时候,我们正在开发一个新的实现,你可以跟踪in this issue的状态。

作为一种解决方法(并实际回答问题),您可以将此行添加到 iOS 上的 LinkerPleaseInclude 文件中(具体使用哪种方法并不重要):var converter = new MvxNativeColorValueConverter();

【讨论】:

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