我在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); }
}
}
}
答案 0 :(得分: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字体库