.Xaml doesn't render unicode that write in the code? - c#

In .xaml of windows store app, If I write like this:
<TextBlock Text="☆" />
It will render a star.
But If I write like this in C# code:
TextBlock tb = new TextBlock
{
Text = "☆"
};
it won't render, why? How to render the unicode write in c# code??

The &...; notation is an XML encoding, there is no decoding applied to C# strings. Use
TextBlock tb = new TextBlock
{
Text = "\x2606";
// Or just:
// Text = "☆";
};

You are using what is called an xmlunicode glyph... see this list of unicode glyphs
To output a stress outlined white star in c# you would specify the unicode as follows:
char c = '\u2729';
System.Console.WriteLine(c.ToString());
See this link for a better description
See this link for a list of unicode characters and their respective codes

Related

How to pass text formatted from a Windows Form to the Word application?

I have a RichTextBox in which I write text and I give it color and format and what I want is that when I press a button that I have programmed, I open the Word application and pass the text that I have in the RichTextBox to the Word document, along with the color and format that I have given in my application.
I have the following code that opens Word and it passes the text that I had in the RichTextBox but the problem is that it does not show me the color and format that I had in the text in my application.
colorLetra = new ColorDialog();
objWord = new Word.Application();
objWord.Visible = true;
objDocumento = objWord.Documents.Add(Missing.Value);
objWord.Selection.Font.Color = objWord.Selection.Font.Color;
objWord.Selection.TypeText(richTextBox.Text);
Could you tell me why it does not show me the color and format in Word?
Your question is:
Could you tell me why it does not show me the color and format in Word?
The reason is because you only enter/type the text. You don't apply any formatting. You're simply transferring the string value of the Windows Forms control to the Word document, as a string.
Your implied question is: How do I pass formatted RichTextBox content to Word...
There is no way to directly pass formatted information from a Windows Form to a Word document. You must go over the clipboard, as was suggested in a comment. The code that comments points to, however, is incorrect for formatted text. The following works for me:
if (richTextBox.Text.Length > 0)
{
// Copy the formatted content to the clipboard
Clipboard.SetText(richTextBox.Rtf, TextDataFormat.Rtf);
objWord.Selection.Paste();
}

Interpret HTML tags in a string and edit the text in the string accordingly [duplicate]

I work on webservice with json and i get text with html format. I want my text have hyperlinks and some other properties where i find from html tags (etc. bold).
I try binding my html string in WebView source but WebView is every time blank. I use this code
var browser = new WebView();
var htmlSource = new HTMLWebViewSource();
htmlSource.Html = MyItem.Article;
browser.Source = htmlSource;
MyItem.Article string is like this
I want something like this inside Label where is inside ListView os something like that.
How can I do this?
This should work for you
string htmlText = MyItem.Article.ToString().Replace(#"\", string.Empty);
var browser = new WebView ();
var html = new HtmlWebViewSource {
Html = htmlText
};
browser.Source = html;
Because Xamarin.Forms.HtmlWebViewSource.HTML expect a pure HTML. Using this you can create a Xamarin.Forms user control with the help of this article http://blog.falafel.com/creating-reusable-xaml-user-controls-xamarin-forms/ Cheers..!
In XAML you can do something like this:
<WebView>
<WebView.Source>
<HtmlWebViewSource Html="{Binding HtmlText}"/>
</WebView.Source>
</WebView>
You may also need to provide Height and Width of the WebView if it's not inside a Grid.
FYI, I've just added the ability to my Forms9Patch library to create labels and buttons where you can format the text via HTML. For example:
new Forms9Patch.Label { HtmlText = "plain <b><i>Bold+Italic</i></b> plain"}
... would give you a label where the text has been formatted bold italic in the middle of the string.
Also, as an aside, it allows you to use custom fonts that are embedded resources in your PCL project without any platform specific work. And, you can use these fonts via the HTLM <font> tag or and HTML font-family attribute.
Here are some screen shots from the demo app:
<pre>
<WebView VerticalOptions="FillAndExpand">
<WebView.Source>
<HtmlWebViewSource Html="{Binding HtmlText}"/>
</WebView.Source>
</WebView>
</pre>

What to use to dispaly formatted text in Xamarin IOS?

I want to display a formatted text in Xamarin IOS, c#. That means a text with special font and also links, like in the picture.
http://i.stack.imgur.com/5BSpH.jpg
I want to use just one TextView, or something else, but the lines must be continoulosly in the control.
Which controls, or methods of TextView should I use?
You can style text in iOS with NSAttributedString
You want to used some form of attributed string. You can use either NSAttributedString or NSMutableAttributedString. There may be some other forms. The mutable strings allow you to change the contents and style after creation and have different styles along the length of the string (for example "Hello World" could have "Hello" be in black and "World" be in red). I'm going to assume you're going to want to use a NSMutableString considering you will be using multiple different styles in one line.
Here's a quick example on how to use it.
NSDictionary format = new NSDictionary (
UIStringAttributeKey.Font, UIFont.FromName ("FontName", 20),
UIStringAttributeKey.ForegroundColor, UIColor.Black
);
NSMutableAttributedString nsString = new NSMutableAttributedString("String", format);
UILabel label = new UILabel();
label.AttributedText = nsString;
Obviously for your application, you're going to have to delve into the Xamarin NSMustableString docs (https://developer.xamarin.com/api/type/MonoTouch.Foundation.NSMutableAttributedString/) in order to fully customize it to your liking, but I think I've given you a decent jumpstart.
NSAttributedString definitely works.
To use custom fonts follow these steps.

What is code equivalent of XAML to preserve whitespace?

What is c# code equivalent of following XAML, where I have a RichTextBox and I have selected Paragraph, and I want to enable/disable white space on this paragraph. In XAML I know how to enable, but I need to do this in code.
<Paragraph xml:space=\"preserve\"> Tabbed Code</Paragraph>
There is an equivalent I have found and its here,
void EnableWhiteSpace(Paragraph p, bool enable = true){
if(enable){
System.Windows.Markup.XmlAttributeProperties
.SetXmlSpace(this.Document, "preserve");
}
else{
System.Windows.Markup.XmlAttributeProperties
.SetXmlSpace(this.Document, "default");
}
}
This is still not working !!! I am not getting tabs !!...
Here is my problem, I have a RichTextBox which is used to edit code and which does syntax highlighting. Everything is fine except when I call following I see no tabs in my code.
TextRange tr = new TextRange(
myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
string text = tr.Text;
The text that I receive contains no tabs, so I thought enabling whitespace on every paragraph before doing text range might give me tabs.
UPDATE
I tried navigating every inlines (run) in the paragraph, none contains tab, I am just loosing all the tabs :(
There is no equivalent. The XML option xml:space="preserve" is valid only for interpreting XML files (in your case, XAML, which is a kind of XML), and has no meaning in C# as there are no XML files involved.
The C# equivalent of your XAML code would be following:
Paragraph p = new Paragraph();
p.Inlines.Add(new Run(" Tabbed Code"));
RichTextBox is buggy, it will just ignore all tabs, however its problem of WPF itself and cant be fixed without doing complex workarounds.
Alernative is to write your own FlowDocument to text converter, however this is very complex as nodes do not directly give any line or tag information.

WPF writing html to a rich text box

I am working on a WPF Windows application using C# code. This is an application I inherited. I only have limited experience using WPF.
I have a Rich Text Box control which is used to build an Email using html. There is code to read the Rich Text Box contents and store the results as html in a database. This code is working. I need to write the reverse that is writing the html to the rich text box so it appears as text.
Below is the code I have to read the rich text box.
TextRange xaml = new TextRange(EmailBodyRichTextBox.Document.ContentStart, EmailBodyRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
xaml.Save(ms, DataFormats.Xaml);
string xamlString = ASCIIEncoding.Default.GetString(ms.ToArray());
string html = HTMLConverter.HtmlFromXamlConverter.ConvertXamlToHtml("<FlowDocument>" + xamlString + "</FlowDocument>");
html = HttpUtility.HtmlEncode(html);
From the code you post above , it seems , you are saving the text in the rich text box to xaml, and then translate the xaml to HTML.
if you want to reverse, then translate your HTML to xaml and then load it to the rich text box.
To save the RichTextBox's content to a regular string that you can save in a database:
string formattedEmail = XamlWriter.Save(EmailBodyRichTextBox.Document);
To reload the RichTextBox from the string:
EmailBodyRichTextBox.Document = XamlReader.Parse(formattedEmail);
If you only want to store the emails in the database and then reload them to RichTextBoxes, there is no point in converting to HTML, it can only introduce conversion format mismatches.

Categories

Resources