Xaml在将FontWeight设置为文本时解析异常。 [英] Xaml parse exception while setting FontWeight to a text.

查看:66
本文介绍了Xaml在将FontWeight设置为文本时解析异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在我的示例中使用了自定义文本框并创建了一个FontWeight类型的属性来设置文本的字体粗细,但我得到了xaml解析例外。

< local:TextBoxEx Height =" 40"宽度= QUOT; 292"前景= QUOT;红色" 
Text =" Annotation" FontWeightEx = QUOT; ExtraBold"
背景="白色>>

< / local:TextBoxEx>

公共类TextBoxEx:TextBox
{

public TextBoxEx()
{
this.FontWeight = FontWeightEx;
}


public FontWeight FontWeightEx
{
get {return(FontWeight)GetValue(FontWeightExProperty); }
set {SetValue(FontWeightExProperty,value); }
}

//使用DependencyProperty作为FontWeightEx的后备存储。这支持动画,样式,绑定等...
public static readonly DependencyProperty FontWeightExProperty =
DependencyProperty.Register(" FontWeightEx",typeof(FontWeight),typeof(TextBoxEx),
new PropertyMetadata(FontWeights.Bold));
}



解决方案

可能你必须转换字符串(例如"ExtraBold")到 FontWeight 结构,反之亦然。基于

https:// social.msdn.microsoft.com/Forums/vstudio/en-US/e678e5bb-31e2-4a85-a8dd-fce502a5aefe
,这是一个例子:

 string weight =" ExtraBold" ;; 
FontWeight font_weight =(FontWeight)new FontWeightConverter()。ConvertFromString(weight);


或者你可以根据'使用自己的转换开关'


Hi,

I have used custom textbox in my sample and created a property of type FontWeight to set  font weight to the text, but I am getting xaml parse exception.

  <local:TextBoxEx  Height="40" Width="292" Foreground="Red" 
                          Text="Annotation"  FontWeightEx="ExtraBold"
                         Background="White">
            
        </local:TextBoxEx>

  public class TextBoxEx : TextBox
    {

        public TextBoxEx()
        {            
            this.FontWeight = FontWeightEx;            
        }


  public FontWeight FontWeightEx
        {
            get { return (FontWeight)GetValue(FontWeightExProperty); }
            set { SetValue(FontWeightExProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FontWeightEx.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FontWeightExProperty =
            DependencyProperty.Register("FontWeightEx", typeof(FontWeight), typeof(TextBoxEx),
            new PropertyMetadata(FontWeights.Bold));
 }


解决方案

Probably you have to convert the strings (such as "ExtraBold") to FontWeight structures and viceversa. Based on https://social.msdn.microsoft.com/Forums/vstudio/en-US/e678e5bb-31e2-4a85-a8dd-fce502a5aefe, here is an example:

string weight = "ExtraBold";
FontWeight font_weight = (FontWeight)new FontWeightConverter().ConvertFromString(weight);

Or you can use your own conversion based on ‘switch’.


这篇关于Xaml在将FontWeight设置为文本时解析异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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