Windows Phone Developers

Monday, June 17, 2013

How to Add/Load XAML Dynamically to RichTextBox Control - Windows Phone Development C#/VB.NET

Windows Phone Development - Populate RichTextBox Content from External XAML File / How to Read Content from External Resource File


+Windows Phone Developers are under pressure to release many updates time to time. Please have a look at the sample application that we have been asked to develop. This particular application is about Tiruvannamalai which houses Ashrams of +yogi ram suratkumar , +Ramana Maharshi , +seshadri swamigal and famous for +Girivalam tiruvannamalai .


This place was known to western world, thanks to Paul Brunton. His book - A Search in Secret India: The Classic Work on Seeking a Guru  was a best seller

Okay, we have diverted a lot. This particular application requires lot of content. A look at the App should make you understand. Have tried the following method to separate the content from XAML file and link it when necessary



Here is how it works:

  • Create Content
  • Embed As Resource
  • Create Place Holder (RTB)
  • Retrieve Resource
  • Assign to Control
 
 
 
Creating content is not an easy task. But beyond the scope of our post. Once the content is created, it has to be embedded as a resource file

Create the XAML for the content and save it as a text file and add it to the project's Resource
 
 
 

 
 
 

 
 


Ensure that the Resource is BuildAction is set to Embedded Resource. Once that is completed use +C# or +Visual Basic.NET code
the following code to get the XAML assigned to the RichTextBox using

How to Read the External ResourceFile using .NET (C#/VB.NET)

GetManifestResourceStream method is used to get the content from the embedded file as a string

private void GetXAML()



{
RichTextBox rtb = this.PageContent;

string result = "";

using (Stream stream = Assembly.GetExecutingAssembly()

.GetManifestResourceStream("Tiruvannamalai.Resources.PageContentXAML.txt"))

using (StreamReader reader = new StreamReader(stream,System.Text.Encoding.UTF8))



{

result = reader.ReadToEnd();



}
string xamlOP = @"
"
;

string xamlMid = result;

string xamlCL = "
";


rtb.Xaml = xamlOP + xamlMid + xamlCL ;

}

The Content is sandwiched between Section tags (here as separate variables) and populated dynamically to the Windows Phone. I tested this using +Nokia Lumia 710 and looks good.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

1 comment: