我在 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); }
}
}
}