我在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) 我想创建简单的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) 我开始使用以下方法分组来使我的代码更具可读性:
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 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) 我有一个带有 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) c# ×3
asp.net-core ×2
flowdocument ×2
wpf ×2
.net ×1
.net-8.0 ×1
bearer-token ×1
binding ×1
jwt ×1
richtextbox ×1
string ×1