【问题标题】:Error setting a style in code for the HintStyle on the PhoneTextBox在 PhoneTextBox 上的 HintStyle 代码中设置样式时出错
【发布时间】:2014-09-15 16:44:22
【问题描述】:

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

$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 中存在的复杂情况。

我的目标只是能够改变提示颜色。有什么想法可以使这项工作或使用其他方法吗?

【问题讨论】:

  • 您要更改 SearchBarWindowsPhone 实现的前景文本颜色吗?如果不是,您要更改哪个控件的外观?
  • 不,这是为了更改提示文本颜色。但是控件是 Windows PhoneToolkit PhoneTextBox。这有一个名为 HintStyle 的属性,它接受一个 Style 对象。既然您已经问过了,我可以看到 Styles TargetType 应该是 ContentControl 而不是 PhoneTextBox。已经整理好了...感谢您让我检查 ;-)

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


【解决方案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,并且没有也会以无益的方式出错!

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2012-02-22
      • 2023-03-31
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多