首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c#解析字体错误

c#解析字体错误
EN

Stack Overflow用户
提问于 2013-02-01 12:43:51
回答 1查看 499关注 0票数 0

我有一个两个窗口的基本系统,不是使用System.Windows.Forms,而是System.Windows.Controls,其中一个创建了FontDialog,所以我必须包括System.Windows.Forms,因为否则我就不能有字体对话框。在另一个窗口中,我有一个RichTextBox,它应该使用在另一个窗口的FontDialog中选择的字体/大小/样式。

这是用于创建FontDialog的窗口的类:

代码语言:javascript
运行
复制
public partial class Window1 : Window
{
    public FontDialog font;

    public Window1(String name)
    {
        InitializeComponent();
        this.textBox1.Text = name;
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        font = new FontDialog();
        font.ShowDialog();
    }
}

在这里,我调用Window1并尝试使用它的字体。

代码语言:javascript
运行
复制
private void button2_Click(object sender, RoutedEventArgs e)
{
    Window1 a = new Window1(this.name);
    a.ShowDialog();

    var cvt = new FontConverter();
    string s = cvt.ConvertToString(a.font.Font);

    Console.Out.WriteLine("Value is: " + s);

    System.Windows.Media.FontFamily g = (System.Windows.Media.FontFamily) cvt.ConvertFromString(s);

    if (g != null)
    {
         this.textBox2.FontFamily = g;
    }
}

它准确地输出在FontDialog中选择的内容,但随后在"this.textBox2.FontFamily =g;“行崩溃:

代码语言:javascript
运行
复制
Value is: Microsoft Sans Serif; 8,25pt
'_.NetworkingGT_Incrementer.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase.resources\v4.0_4.0.0.0_pt-BR_31bf3856ad364e35\WindowsBase.resources.dll'
A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
'_.NetworkingGT_Incrementer.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml.resources\v4.0_4.0.0.0_pt-BR_b77a5c561934e089\System.Xaml.resources.dll'
A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
EN

Stack Overflow用户

回答已采纳

发布于 2013-02-01 13:21:38

已解决:

代码语言:javascript
运行
复制
var cvt = new FontConverter();

string s = cvt.ConvertToString(a.font.Font);

Console.Out.WriteLine("Value is: " + s);

System.Windows.Media.FontFamily g = new System.Windows.Media.FontFamily(s);

Console.Out.WriteLine("Value is: " + g.ToString());

this.textBox2.FontFamily = g;

两个输出是相等的。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14639876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档