小编Art*_*luk的帖子

替换XamlPackage中的文本

我在RichTextBox中有一些文本.此文本包含标签,例如:[@TagName!].我想用数据库中的一些数据替换这些标签而不会丢失格式(字体,颜色,图像等).我创建了一个方法:

 void ReplaceTagsWithData(FlowDocument doc)
    {
        FileStream fs = new FileStream("tmp.xml", FileMode.Create);
        TextRange trTextRange = 
            new TextRange(doc.ContentStart, doc.ContentEnd);

        trTextRange.Save(fs, DataFormats.Xaml);
        fs.Dispose();
        fs.Close();

        StreamReader sr = new StreamReader("tmp.xml");

        string rtbContent = sr.ReadToEnd();

        MatchCollection mColl = 
            Regex.Matches(rtbContent, 
                          string.Format(@"\{0}+[a-zA-Z]+{1}", 
                          prefix, 
                          postfix));

        foreach (Match m in mColl)
        {
            string colname = 
                m.Value.Substring(prefix.Length, 
                   (int)(m.Value.Length - (prefix.Length + postfix.Length)));

            rtbContent = rtbContent.Replace(m.Value.ToString(), 
                                            dt.Rows[0][colname].ToString());
        }

        rtbEdit.Document = 
            new FlowDocument(
                (Section)XamlReader.Load(
                    XmlReader.Create(new StringReader(rtbContent))));
        sr.Dispose();
        sr.Close();
    }
Run Code Online (Sandbox Code Playgroud)

它非常好,但它从内容中删除图像.我知道我应该使用XamlPackage而不是Xaml但是我不能把它作为纯文本.还有其他解决方案吗?

谢谢你的回答.;)

[编辑:2012年2月13日02:14(上午)]

我的工作方案:

    void ReplaceTagsWithData(RichTextBox rtb)
{
    FlowDocument doc = rtb.Document;

    FileStream …
Run Code Online (Sandbox Code Playgroud)

c# string wpf flowdocument

41
推荐指数
1
解决办法
2067
查看次数

代码生成器错误:DbContext上没有实体类型

我想创建简单的ASP.NET核心应用程序.但是,当我尝试为我的模型生成API控制器时,我遇到以下错误:(屏幕截图显示模型类和数据库上下文的代码,下面是EntityBase类的代码)

设置代码脚手架属性

错误

public abstract class EntityBase
{
    [Key]
    public int Id { get; set; }
    public DateTime Modified { get; set; }
    public DateTime Created { get; set; }

    public EntityBase()
    {
        Created = DateTime.Now;
        Modified = Created;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# entity-framework asp.net-core

6
推荐指数
1
解决办法
2725
查看次数

归还财产

我开始使用以下方法分组来使我的代码更具可读性:

public interface IUserMethodGroup
{
   void SomeMethodOne();
   String SomeMethodTwo();
}

//File MainClass.Users.cs
public partial class MainClass : IUserMethodGroup
{
   void IUserMethodGroup.SomeMethodOne(){ //some code }
   String IUserMethodGroup.SomeMethodTwo(){ return "some str";}
}

//File MainClass.cs
public partial class MainClass
{
    //HOW ABOUT PERFORMANCE?
    public IUserMethodGroup UserHandle {get { return this; } }       
}
Run Code Online (Sandbox Code Playgroud)

它对性能有很大影响吗?

编辑1

这允许我这样做:

class ConsumerClass
{
   public void Method()
   {
      MainClass mc = new MainClass();

      mc.UserHandle.SomeMethodOne();

      //@ vc 74:
      //Interface has to be explicitly, otherwise it will be:
      ms.SomeMethodOne(); //grouping …
Run Code Online (Sandbox Code Playgroud)

.net c#

5
推荐指数
1
解决办法
1187
查看次数

自定义令牌验证器在 .NET 8 中不起作用

今天我正在检查是否可以将我的应用程序迁移到 .NET 8。起初一切正常,当我尝试登录时,我在控制台中收到以下错误:

无法验证令牌。
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10506:签名验证失败。在 TokenValidationParameters 上指定的用户定义的“委托”未返回“Microsoft.IdentityModel.JsonWebTokens.JsonWebToken”,但在验证令牌时返回“System.IdentityModel.Tokens.Jwt.JwtSecurityToken”

我的客户正在使用 PingID 的定制实现。使用内省来验证令牌,这就是我编写自定义令牌验证器的原因:

public class PingTokenValidator : ISecurityTokenValidator
{
    private readonly IConfiguration configuration;
    private readonly ILogger log;
    private readonly OpenIdConnectConfiguration openIdConnectConfiguration;
    private readonly JwtSecurityTokenHandler tokenHandler;

    public PingTokenValidator(OpenIdConnectConfiguration openIdConnectConfiguration,
        IConfiguration configuration)
    {
        this.tokenHandler = new();
        this.openIdConnectConfiguration = openIdConnectConfiguration;
        this.configuration = configuration;
        this.log = Log.ForContext<PingTokenValidator>();
    }

    public bool CanReadToken(string securityToken)
    {
        return true;
    }

    public ClaimsPrincipal ValidateToken(string securityToken,
        TokenValidationParameters validationParameters,
        out SecurityToken validatedToken)
    {
        try
        {
            var principal = this.tokenHandler.ValidateToken(securityToken, validationParameters, out validatedToken);

            if (tokenExistInCache …
Run Code Online (Sandbox Code Playgroud)

jwt bearer-token asp.net-core asp.net-core-webapi .net-8.0

4
推荐指数
1
解决办法
1679
查看次数

DocumentViewer 到 RichTextBox 绑定错误

我有一个带有 RichTextBox 和 DocumentViewer 的应用程序(放置在 TabControl 中),我想制作类似“热预览”的内容。我已将DocumentViewer.Document财产绑定到RichTextBox.Document

捆绑:

<DocumentViewer Document="{Binding Document, Converter={StaticResource FlowDocumentToPaginatorConverter}, ElementName=mainRTB, Mode=OneWay}" />

这是转换器代码:

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            FlowDocument d = value as FlowDocument;
            DocumentPaginator pagin = ((IDocumentPaginatorSource)d).DocumentPaginator;
            FixedDocumentSequence result = null;
            Size s = new Size(793.700787402, 1122.519685039);
            pagin.PageSize = s;

            using (MemoryStream ms = new MemoryStream())
            {
                TextRange tr = new TextRange(d.ContentStart, d.ContentEnd);
                tr.Save(ms, DataFormats.XamlPackage);
                Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
                Uri uri = new Uri(@"memorystream://doc.xps"); …
Run Code Online (Sandbox Code Playgroud)

wpf binding richtextbox documentviewer flowdocument

2
推荐指数
1
解决办法
1470
查看次数