在PhoneTextBox上为HintStyle的代码设置样式时出错

时间:2014-09-15 16:44:23

标签: c# windows-phone-8 xamarin xamarin.forms

由于各种原因,我试图在代码中创建和设置样式,但是我得到以下应用程序未处理的异常。

$exception  {System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at Xamarin.Forms.Platform.WinPhone.EntryRenderer.GetDesiredSize(Double widthConstraint, Double    heightConstraint)
   at Xamarin.Forms.Platform.WinPhone.Platform.GetNativeSize(VisualElement view, Double widthConstraint, Double heightConstraint)
   at Xamarin.Forms.VisualElement.OnSizeRequest(Double widthConstraint, Double heightConstraint)
   at Xamarin.Forms.VisualElement.GetSizeRequest(Double widthConstraint, Double heightConstraint)
   at Xamarin.Forms.StackLayout.SumOfSizeRequests(Double widthConstraint, Double heightConstraint, Int32& numOfExpanders)
   at Xamarin.Forms.StackLayout.LayoutChildren(Double x, Double y, Double width, Double height)
   at Xamarin.Forms.Layout.UpdateChildrenLayout()
   at Xamarin.Forms.Layout.OnSizeAllocated(Double width, Double height)
   at Xamarin.Forms.VisualElement.SizeAllocated(Double width, Double height)
   at Xamarin.Forms.Layout.OnChildMeasureInvalidated(Object sender, EventArgs e)
   at Xamarin.Forms.VisualElement.InvalidateMeasure()
   at Xamarin.Forms.VisualElement.NativeSizeChanged()
   at Xamarin.Forms.VisualElement.set_IsNativeStateConsistent(Boolean value)
   at Xamarin.Forms.Platform.WinPhone.VisualElementRenderer`2.<SetNativeControl>b__1(Object sender, RoutedEventArgs args)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)} System.Exception

我写的代码是

var hintStyle = new Style(typeof(PhoneTextBox));
hintStyle.Setters.Add(
   new Setter(
              System.Windows.Controls.Control.ForegroundProperty,
              view.PlaceholderTextColor.ToBrush())
              );
phoneTextBox.HintStyle = hintStyle;

这样运行没有错误,但稍后(我假设在某个渲染点)发生错误。这是一个复杂的问题,这是在Xamarin Forms Renderer中。

我的目标是能够改变提示颜色。任何想法如何使这项工作或使用另一种方法?

2 个答案:

答案 0 :(得分:1)

上面的评论让我仔细检查了我的代码,发现Styles TargetType不正确。正确的目标类型是ContentControl,更正的代码是

var hintStyle = new Style(typeof(ContentControl));
hintStyle.Setters.Add(
   new Setter(
          System.Windows.Controls.Control.ForegroundProperty,
          view.PlaceholderTextColor.ToBrush())
          );
phoneTextBox.HintStyle = hintStyle;

这里的课程总是确保你有正确的TargetType,并且没有任何一个也会以无用的方式出错!

答案 1 :(得分:0)

我收到了相同的错误消息,对我而言,原因是我的Windows Phone项目没有正确安装Xamarin.Forms包。在搜索了几个小时之后,我通过NuGet重新安装它并且它有效。

我知道这不是这个特定问题的解决方案(因为它已经解决了)但这是我在搜索问题的解决方案时找到的第一个线程。希望它可以帮到某人。