Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#FlowDocument RichTextBox对齐不会持续_C#_Xaml_Alignment_Richtextbox - Fatal编程技术网

C#FlowDocument RichTextBox对齐不会持续

C#FlowDocument RichTextBox对齐不会持续,c#,xaml,alignment,richtextbox,C#,Xaml,Alignment,Richtextbox,我继承了一些C#代码,它定义了一个RichTextBox,带有用于文本左对齐、居中对齐和右对齐的按钮。运行程序时,使用“居中”按钮使文本居中,但当我保存文件并重新加载时,居中消失 将保留字体大小、样式和颜色等格式。当我查看Xaml文件输出时,我看到的是“TextAlignment=Justify”,而不是“TextAlignment=Center”。我使用的是Visual C#2010,如果有人能够验证这是一个已在更高版本中修复的问题,我将很乐意升级 代码如下: private void

我继承了一些C#代码,它定义了一个
RichTextBox
,带有用于文本左对齐、居中对齐和右对齐的按钮。运行程序时,使用“居中”按钮使文本居中,但当我保存文件并重新加载时,居中消失

将保留字体
大小
样式
颜色
等格式。当我查看Xaml文件输出时,我看到的是
“TextAlignment=Justify”
,而不是
“TextAlignment=Center”
。我使用的是Visual C#2010,如果有人能够验证这是一个已在更高版本中修复的问题,我将很乐意升级

代码如下:

    private void save(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.DialogResult res;
        if (saveas || Properties.Settings.Default.fr == "" || Properties.Settings.Default.fr == null)
        {
            System.Windows.Forms.SaveFileDialog of = new System.Windows.Forms.SaveFileDialog();
            of.Filter = "Story files (*.sw)|*.sw|User template files (*.ut)|*.ut";
            of.Title = "Save File";
            if (Properties.Settings.Default.currentFileType.Equals("SYSTEM_TEMPLATE") || Properties.Settings.Default.currentFileType.Equals("USER_TEMPLATE"))
            {
                of.FilterIndex = 2;
            }
            try
            {
                of.FileName = Properties.Settings.Default.fr.Split("\\".ToCharArray())
                    [Properties.Settings.Default.fr.Split("\\".ToCharArray()).Length - 1];
            }
            catch { };
            res = of.ShowDialog();
            Properties.Settings.Default.fr = of.FileName;
            Properties.Settings.Default.Save();
        }
        else
        {
            res = System.Windows.Forms.DialogResult.OK;
        }
        FlowDocument fd = new FlowDocument();
        if (res == System.Windows.Forms.DialogResult.OK)
        {
            Properties.Settings.Default.currentFileType = getFileTypeFromExtension(System.IO.Path.GetExtension(Properties.Settings.Default.fr.ToString()));
            Properties.Settings.Default.Save();


            fd = toMS();
            TextRange range;
            System.IO.FileStream fStream;
            range = new TextRange(fd.ContentStart, fd.ContentEnd);
            try
            {
                fStream = new System.IO.FileStream(Properties.Settings.Default.fr, System.IO.FileMode.Create);
                range.Save(fStream, DataFormats.Rtf);
                fStream.Close();
                Application.Current.MainWindow.Title = "StoryWeaver: " + Properties.Settings.Default.fr;

            }
            catch
            {
                MessageBox.Show("File not available. \n Try closing all programs that are using the file.");
            }
        }
    }

    public FlowDocument toMS()
    {
        FlowDocument fd = new FlowDocument();
        fd.Blocks.Add(new Paragraph(new Run("(%&Version 1&%)")));
        fd.Blocks.Add(new Paragraph(new Run("(%&ui&%)")));
        fd.Blocks.Add(new Paragraph(new Run("Tab: 0")));
        fd.Blocks.Add(new Paragraph(new Run("Cards: 0")));
        fd.Blocks.Add(new Paragraph(new Run("Tree: True")));
        fd.Blocks.Add(new Paragraph(new Run("(%&StoryWeaver&%)")));
        fd.Blocks.Add(new Paragraph(new Run("")));
        AddDocument(toDocument(), fd);
        return fd;
    }

    public FlowDocument toDocument()
    {
        tree_SelectedItemChanged(null, null);
        FlowDocument fd = new FlowDocument();
        AddBlock(new Paragraph(new Run(getIndex(tree.SelectedItem as TreeViewItem).ToString() + "\n")), fd);
        fd.Blocks.Add(new Paragraph());
        string[] temp;
        int i, j;
        for (i = 0; i < tree.Items.Count; i++)
        {
            AddBlock(new Paragraph(new Run(((tree.Items[i] as TreeViewItem).Header as TextBox).Text + "\t"
                                            + (tree.Items[i] as TreeViewItem).Tag as string)), fd);
            fd.Blocks.Add(new Paragraph());
            temp = subTMS(tree.Items[i] as TreeViewItem).Split('\n');
            for (j = 0; j < temp.Length; j++)
            {
                if (temp[j].Trim('\t').Length > 0)
                {
                    AddBlock(new Paragraph(new Run("\t" + temp[j])), fd);
                    fd.Blocks.Add(new Paragraph());
                }
            }
        }
        for (i = 0; i < promptPages.Count; i++)
        {
            AddBlock(new Paragraph(new Run("//-------------------------------------")), fd);
            fd.Blocks.Add(new Paragraph());
            AddDocument(promptPages[i], fd);
            AddBlock(new Paragraph(new Run("//-------------------------------------")), fd);
            fd.Blocks.Add(new Paragraph());
            if (Properties.Settings.Default.currentFileType.Equals("USER_TEMPLATE"))
            {

                pages[i].Blocks.Clear();
            }
            AddDocument(pages[i], fd);


       }
        return fd;
    }

   public static void AddDocument(FlowDocument from, FlowDocument to)
    {
        TextRange range = new TextRange(from.ContentStart, from.ContentEnd);

        System.IO.MemoryStream stream = new System.IO.MemoryStream();

        System.Windows.Markup.XamlWriter.Save(range, stream);

        range.Save(stream, DataFormats.XamlPackage);

        TextRange range2 = new TextRange(to.ContentEnd, to.ContentEnd);

        range2.Load(stream, DataFormats.XamlPackage);
    }

    /// <summary>
    /// Adds a block to a flowdocument.
    /// </summary>
    /// <param name="from">From.</param>
    /// <param name="to">To.</param>
    public static void AddBlock(Block from, FlowDocument to)
    {
        if (from != null)
        {
            TextRange range = new TextRange(from.ContentStart, from.ContentEnd);

            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            System.Windows.Markup.XamlWriter.Save(range, stream);

            range.Save(stream, DataFormats.XamlPackage);

            TextRange textRange2 = new TextRange(to.ContentEnd, to.ContentEnd);

            textRange2.Load(stream, DataFormats.XamlPackage);
        }
    }
private void save(对象发送方,RoutedEventArgs e)
{
System.Windows.Forms.DialogResult;
如果(另存为| | Properties.Settings.Default.fr==“”| | Properties.Settings.Default.fr==null)
{
System.Windows.Forms.SaveFileDialog of=新建System.Windows.Forms.SaveFileDialog();
of.Filter=“故事文件(*.sw)|*.sw |用户模板文件(*.ut)|*.ut”;
of.Title=“保存文件”;
if(Properties.Settings.Default.currentFileType.Equals(“系统_模板”)|| Properties.Settings.Default.currentFileType.Equals(“用户_模板”))
{
of.FilterIndex=2;
}
尝试
{
of.FileName=Properties.Settings.Default.fr.Split(“\\”.ToCharArray())
[Properties.Settings.Default.fr.Split(“\\”.ToCharArray()).Length-1];
}
捕获{};
res=of.ShowDialog();
Properties.Settings.Default.fr=of.FileName;
Properties.Settings.Default.Save();
}
其他的
{
res=System.Windows.Forms.DialogResult.OK;
}
FlowDocument fd=新的FlowDocument();
if(res==System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.currentFileType=getFileTypeFromExtension(System.IO.Path.GetExtension(Properties.Settings.Default.fr.ToString());
Properties.Settings.Default.Save();
fd=toMS();
文本范围;
System.IO.FileStream-fStream;
范围=新文本范围(fd.ContentStart,fd.ContentEnd);
尝试
{
fStream=new System.IO.FileStream(Properties.Settings.Default.fr,System.IO.FileMode.Create);
Save(fStream,DataFormats.Rtf);
fStream.Close();
Application.Current.MainWindow.Title=“StoryWeaver:+Properties.Settings.Default.fr;
}
抓住
{
Show(“文件不可用。\n请尝试关闭使用该文件的所有程序。”);
}
}
}
公共流程文档toMS()
{
FlowDocument fd=新的FlowDocument();
fd.Blocks.Add(新段落(新运行(((&版本1&%)));
fd.Blocks.Add(新段落(新运行(((&ui&%)));
fd.Blocks.Add(新段落(新段落(“制表符:0”));
fd.Blocks.添加(新段落(新运行(“卡片:0”));
添加(新段落(新运行(“树:真”));
fd.Blocks.Add(新段落(新段落(((&StoryWeaver&%))));
fd.Blocks.Add(新段落(新段落(“”));
添加文档(toDocument(),fd);
返回fd;
}
公共流程文档到文档()
{
树\u SelectedItemChanged(null,null);
FlowDocument fd=新的FlowDocument();
AddBlock(新段落(新运行(getIndex(tree.SelectedItem as TreeViewItem.ToString()+“\n”)),fd);
添加(新段落());
字符串[]温度;
int i,j;
对于(i=0;i0)
{
AddBlock(新段落(新运行(“\t”+temp[j])),fd;
添加(新段落());
}
}
}
对于(i=0;i  public static void AddBlock(Block from, FlowDocument to)
  {
    if (from != null)
    {
        TextRange range = new TextRange(from.ContentStart, from.ContentEnd);

        System.IO.MemoryStream stream = new System.IO.MemoryStream();

        System.Windows.Markup.XamlWriter.Save(range, stream);

        range.Save(stream, DataFormats.XamlPackage);

        TextRange textRange2 = new TextRange(to.ContentEnd, to.ContentEnd);

        textRange2.Load(stream, DataFormats.XamlPackage);
    }
}
          TextAlignment blockTextAlignment = (TextAlignment)range.GetPropertyValue(TextBlock.TextAlignmentProperty);
          textRange2.ApplyPropertyValue(TextBlock.TextAlignmentProperty, blockTextAlignment);