Skip to main content
 首页 » 编程设计

c#之无法将 MvvMCross 6.x iOS 颜色绑定(bind)到 MvxColor WithConversion

2024年02月25日73kuangbin

我是 MvvMCross 的新手,我在将我的 View 模型 mvx 属性绑定(bind)到 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 6 带有一个注册插件的新机制。

我们遇到了两种问题:

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

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

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

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