MvvmCross在Windows Phone Designer中引发NullReferenceException异常

3
我在设计器中使用了MVVMCross的可见性转换器后出现以下错误,无法使用Blend来处理我的UI。有任何想法吗?
NullReferenceException: Object reference not set to an instance of an object.

at Cirrious.CrossCore.Mvx.Resolve[TService]()
at Cirrious.MvvmCross.Plugins.Visibility.MvxBaseVisibilityValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at Cirrious.CrossCore.WindowsPhone.Converters.MvxNativeValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertToTarget(Object value)
at System.Windows.Data.BindingExpression.GetValue(DependencyObject d, DependencyProperty dp)
at System.Windows.DependencyObject.EvaluateExpression(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry)
at System.Windows.DependencyObject.EvaluateBaseValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.EvaluateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry newEntry, ValueOperation operation)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
at System.Windows.Data.BindingExpression.SendDataToTarget()
at System.Windows.Data.BindingExpression.SourceAcquired()
at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)
at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent, Byte bCanCreateParent)

感谢您,MagooChris。
1个回答

2
跨平台可见性功能是通过插件实现的,因此在使用之前需要初始化一些IoC系统。如果要与转换器一起使用设计器,则需要添加少量的设计时初始化。对于BindingEx模块,我们使用以下方式进行初始化:https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.BindingEx.WindowsPhone/MvxDesignTimeChecker.cs#L18。如果您需要可见性插件的设计时支持,则可以使用类似的设计时检查,并确保可见性转换已在IoC中注册。例如,在Windows Phone中,您可以将以下内容插入到静态资源中:
if (!DesignerProperties.IsInDesignTool)
  return;

if (MvxSingleton<IMvxIoCProvider>.Instance == null)
{
  var iocProvider = MvxSimpleIoCContainer.Initialize();
  Mvx.RegisterSingleton(iocProvider);
}

var forceVisibility = new Cirrious.MvvmCross.Plugins.Visibility.WindowsPhone.Plugin();
forceVisibility.Load();

注意:显然这有点hacky - 希望能将其拉回项目中,以便所有人都可以使用。

还有跟进 https://github.com/slodge/MvvmCross/issues/323 - 请在那里添加评论/愿望/改进。 - Stuart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接