Xamarin.Forms UWP - 尝试为 TextBlock 设置 TextDecorations 时出现 InvalidCastException [英] Xamarin.Forms UWP - InvalidCastException when trying to set TextDecorations for TextBlock

查看:26
本文介绍了Xamarin.Forms UWP - 尝试为 TextBlock 设置 TextDecorations 时出现 InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xamarin.Forms 项目中,我试图允许带下划线的标签.所以我有一个自定义渲染器,我正在尝试做一些简单的事情:

Control.TextDecorations = TextDecorations.Underline;

它编译得很好,但是当应用程序启动时,我在该行上收到一个 InvalidCastException,它说:

<块引用>

System.InvalidCastException:无法将类型为Windows.UI.Xaml.Controls.TextBlock"的对象转换为类型Windows.UI.Xaml.Controls.ITextBlock5".

这是异常的屏幕截图:

此外,在检查控件时,我注意到 TextBlock 控件的其他属性也存在大量 InvalidCastException 异常 - 这是一个小示例:

为什么它试图转换为 ITextBlock5 类型?这是 UWP 错误吗?有没有办法让下划线起作用?

解决方案

根据 Microsoft 文档,直到版本 15063 才引入 TextDecorations 属性.您可能会遇到该异常,因为您使用的是早期版本的 Windows.>

作为一种解决方法,您可以创建一个 Underline() 对象并将一个 Run() 对象添加到 Underline 对象的 Inlines 集合中,如下所示:

//先清除控件内容Control.Inlines.Clear();//接下来创建新的 Underline 对象和//添加一个新的 Run 到它的 Inline 集合var underlinedText = new Underline();underlinedText.Inlines.Add(new Run { Text = "xamarin 元素的文本"});//最后添加新的 Underline 对象//到控件的内联集合Control.Inlines.Add(underlinedText);

在自定义渲染器的 OnElementChanged() 覆盖方法中,它看起来像这样:

protected override void OnElementChanged(ElementChangedEventArgs

In a Xamarin.Forms project, I'm trying to allow underlined Labels. So I have a custom renderer, and I'm trying to do something simple:

Control.TextDecorations = TextDecorations.Underline;

It compiles just fine, but when the app launches, I'm getting an InvalidCastException on that line which says:

System.InvalidCastException: 'Unable to cast object of type 'Windows.UI.Xaml.Controls.TextBlock' to type 'Windows.UI.Xaml.Controls.ITextBlock5'.'

Here's a screenshot of the exception:

Also, when inspecting the Control, I noticed there are a ton of InvalidCastException exceptions on other properties of the TextBlock control as well - here's a small sample:

Why is it trying to cast to a ITextBlock5 type? Is this a UWP bug? Is there a workaround to get underline to work?

解决方案

According to the Microsoft documentation, the TextDecorations property wasn't introduced until version 15063. You're probably getting that exception because you're on an earlier version of Windows.

As a workaround, you can create an Underline() object and add a Run() object to the Underline object's Inlines collection, like this:

// first clear control content
Control.Inlines.Clear();

// next create new Underline object and
// add a new Run to its Inline collection
var underlinedText = new Underline();
underlinedText.Inlines.Add(new Run { Text = "text of xamarin element here" });

// finally add the new Underline object
// to the Control's Inline collection
Control.Inlines.Add(underlinedText);

Inside the custom renderer's OnElementChanged() override method, it would look something like this:

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
    base.OnElementChanged(e);
    var view = e.NewElement as LabelExt; // LabelExt => name of PCL custom Label class
    var elementExists = (view != null && Control != null);
    if (!elementExists)
    {
        return;
    }

    // add underline to label
    Control.Inlines.Clear();
    var underlinedText = new Underline();
    underlinedText.Inlines.Add(new Run { Text = view.Text });
    Control.Inlines.Add(underlinedText);
}

这篇关于Xamarin.Forms UWP - 尝试为 TextBlock 设置 TextDecorations 时出现 InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆