2

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

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); }
        }
    }
}
4

1 回答 1

3

好的,我知道问题出在哪里,但我不知道如何在程序上解决这个问题。

问题是,FontFamily如果您没有使用它的名称/路径来限定自定义字体,那么指定自定义字体的Fonts\mynewfont.ttf#My New Font. 上面的代码所做的只是说“提出My New Font”,在运行时它无法理解,因为它无法找到。

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

1 Yanone Kaffeesatz Thin来自 Google 字体库

于 2011-01-20T18:53:40.810 回答