【问题标题】:Silverlight Random Font BugSilverlight 随机字体错误
【发布时间】:2011-01-21 02:53:40
【问题描述】:

我在 Silverlight 项目中添加了一些自定义字体。然而,他们的行为非常零星。我在自定义控件中使用它。当我第一次添加控件时,它显示正常。当我重建时,字体变回默认值。当我在浏览器中查看应用程序时,它也使用默认字体。字体作为资源嵌入。

顶部是我将控件添加到设计器之后,底部是浏览器中的应用程序。我不知道是什么原因造成的。如果您需要控件的代码,我可以提供。

BorderedTextBlock.xaml

<UserControl x:Class="MindWorX.CustomPropertyReproduction.Controls.BorderedTextBlock"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="100" Width="200">

    <Grid x:Name="LayoutRoot">
        <Border BorderThickness="1" BorderBrush="Lime">
            <TextBlock Name="MainTextBlock" Margin="4" TextWrapping="Wrap" />
        </Border>
    </Grid>
</UserControl>

BorderedTextBlock.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MindWorX.CustomPropertyReproduction.Controls
{
    public partial class BorderedTextBlock : UserControl
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(BorderedTextBlock), new PropertyMetadata("TextBlock", new PropertyChangedCallback(OnTextChanged)));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.Text = (String)e.NewValue;
        }

        new public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(BorderedTextBlock), new PropertyMetadata(new FontFamily("Portable User Interface"), new PropertyChangedCallback(OnFontFamilyChanged)));

        private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.FontFamily = (FontFamily)e.NewValue;
        }

        new public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(Double), typeof(BorderedTextBlock), new PropertyMetadata(11d, new PropertyChangedCallback(OnFontSizeChanged)));

        private static void OnFontSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.FontSize = (Double)e.NewValue;
        }

        public BorderedTextBlock()
        {
            InitializeComponent();
        }

        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        new public FontFamily FontFamily
        {
            get { return (FontFamily)GetValue(FontFamilyProperty); }
            set { SetValue(FontFamilyProperty, value); }
        }

        new public Double FontSize
        {
            get { return (Double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }
    }
}

【问题讨论】:

  • 如果您可以使用自定义字体添加您正在使用的语法和项目结构,这将有助于解决您的问题。
  • 这是问题的重现。如果您进行测试,您会发现它适用于预先存在的字体。但不是自定义字体。 dl.dropbox.com/u/992656/MindWorX.CustomPropertyReproduction.7z
  • 无法弄清楚 .7z 扩展名是什么,以便将其从保管箱中拉出并查看您的示例。您能否在上面发布您的代码,这将帮助我们确定可能是什么问题?
  • @Otaku:将控件的来源添加到问题中。你必须找到一种自定义字体来自己测试它。

标签: silverlight xaml embedded-fonts


【解决方案1】:

好的,我知道问题出在哪里,但我不知道如何通过程序解决此问题。

问题在于,如果您不使用它的名称/路径(如Fonts\mynewfont.ttf#My New Font)来限定它,那么指定自定义字体的FontFamily 并不重要(即它不起作用)。上面的代码所做的只是说“打开My New Font”,在运行时它无法理解,因为找不到它。

解决此问题的一种方法是将您需要的字体作为资源在 App.xaml 中并以这种方式使用它们,例如:&lt;FontFamily x:Key="YanoneKaffeesatzThin"&gt;Fonts\Yanone Kaffeesatz-47.ttf#Yanone Kaffeesatz Thin&lt;/FontFamily&gt;1 然后从代码中调用该系列的应用程序:myTxtBox.FontFamily = DirectCast(App.Current.Resources("YanoneKaffeesatzThin"), FontFamily).

1Yanone Kaffeesatz Thin 来自 Google 字体库

【讨论】:

  • +1 用于资源 xaml 示例。但是,问题不在于从代码分配时,而是当我使用设计器分配字体时。我制作了另一个自定义控件,不仅实现了自定义字体,还实现了其他 4 个与字体相关的字段。这样做之后,它似乎可以正常工作。我不知道为什么,但看起来它解决了这个问题。
猜你喜欢
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多