How to serialize XAML as part of other plain XML nodes? - wpf

I want to serialize XAML and other app's-settings-info in a single settings-file. Something like:
<root>
<settings>
...
</settings>
<xaml_stuff>
...
</xaml_stuff>
</root>
I'll be thankful for any guidance how to elegantly accomplish it: I have XamlWriter on one hand and the other XML-API\namespaces on the other, but I can't find a way to connect them together.

See if this helps: http://msdn.microsoft.com/en-us/library/ms590446.aspx.
From what I understand, it first serializes it to XmlWriter, which you can then use to add other non-xaml stuff
Edit: Here's a concrete C# example (with some changes to what I said previously)...
XmlTextWriter w = new XmlTextWriter("test.xml", Encoding.UTF8);
w.WriteStartElement("root");
w.WriteAttributeString("xmlns", "x");
w.WriteStartElement("item1");
w.WriteEndElement();
w.WriteStartElement("item2");
w.WriteEndElement();
Button btn = new Button();
btn.Content = "Test Button";
btn.Width = 200;
btn.Height = 100;
btn.Foreground = Brushes.Green;
string buf = XamlWriter.Save(btn);
XmlTextReader reader = new XmlTextReader(new StringReader(buf));
reader.Read();
w.WriteNode(reader, true);
w.WriteEndElement();
w.Flush();
w.Close();
And here are the contents of the file it generates:
<root xmlns="x">
<item1 />
<item2 />
<Button Foreground="#FF008000" Width="200" Height="100" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">Test Button</Button>
</root>

Related

Updating telerik:RadPieChart Palette asynchronously doesn't work

As the title suggests I'm trying to update the Palette of the telerik:RadPieChart asynchronously. I have implemented a Task which tries to check for new records at a given interval and updates the UI if there's any. Every other controls are updating correctly except the Palette of the telerik:RadPieChart. The Palette would disappear after the Task tries to update it. Here is the pie chart I'm working with.
<telerik:RadPieChart Name="PieLinearChart" Foreground="Gainsboro" FontSize="12" FontWeight="Black" Grid.Row="0" Palette="{Binding Tab.CurrentPoC.PalettePie1}" Height="50px">
<telerik:RadPieChart.Series>
<telerik:DoughnutSeries ShowLabels="True" InnerRadiusFactor="0.4" ValueBinding="Sum" ItemsSource="{Binding Tab.CurrentPoC.PieChart1, Mode=TwoWay}" />
</telerik:RadPieChart.Series>
</telerik:RadPieChart>
The telerik:DoughnutSeries values are updating, though.
The code behind looks something like this:
public Task Init()
{
.......
.......
ExchangeFile.DataUpdated += Exchange_DataUpdated;
.......
.......
}
//This method was called every certain interval
//and works from the background.
private void Exchange_DataUpdated()
{
......
......
//Setting colors to the Palette
//which didn't work in this case
//(works well for other situations)
PalettePie1 = new ChartPalette();
ChartPalette tmpPalette = new ChartPalette();
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Red")));
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Blue")));
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Yellow")));
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Green")));
PalettePie1 = tmpPalette;
......
}
I also tried something like this but didn't help:
Application.Current.Dispatcher.Invoke(new Action(() => {
......
ChartPalette tmpPalette = new ChartPalette();
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Red")));
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Blue")));
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Yellow")));
tmpPalette.GlobalEntries.Add(new PaletteEntry(Utilities.NvarBrushUtils.GetBrush("Green")));
PalettePie1 = new ChartPalette();
PalettePie1 = tmpPalette;
......
}));
Please help!

AvalonEdit - xshd for JSON highlighting

Is there an xshd ruleset for the AvalonEdit control to highlight the JSON syntax? I tried the definition for JavaScript, but it doesn't work well, i.e.:
{
"name" : "value"
}
both name and value have the same color using the JavaScript definition.
Is there a ruleset for JSON, and if not, how can I modify the xshd so that I get different colors for the name and value in JSON?
If somebody needs something like that, I worked it out in following way:
<?xml version="1.0" encoding="utf-8" ?>
<SyntaxDefinition name="Json" extensions=".js" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Digits" foreground="#8700FF" exampleText="3.14" />
<Color name="Value" foreground="#000CFF" exampleText="var text = "Hello, World!";" />
<Color name="ParamName" foreground="#057500" exampleText="var text = "Hello, World!";" />
<RuleSet ignoreCase="false">
<Keywords color="Digits" >
<Word>true</Word>
<Word>false</Word>
</Keywords>
<Span color="ParamName">
<Begin>"</Begin>
<End>(?=:)</End>
</Span>
<Span color="Value" multiline="true">
<Begin>
(?<=:)\040"[^"]*
</Begin>
<End>"</End>
</Span>
<Rule color="Digits">\b0[xX][0-9a-fA-F]+|(\b\d+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?</Rule>
</RuleSet>
</SyntaxDefinition>
Not perfect, but for me enough.
Just use AvalonEdit's JavaScript highlighter.
Sample code:
using (var stream = Assembly.GetAssembly(typeof(ICSharpCode.AvalonEdit.TextEditor)).GetManifestResourceStream("ICSharpCode.AvalonEdit.Highlighting.Resources.JavaScript-Mode.xshd"))
{
using (var reader = new XmlTextReader(stream))
{
avalonEdit.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
SearchPanel.Install(avalonEdit);
}
}

Populating a listview contents with an array

I have created a function that would be able to place the array contents into a listview, Here is my progress so far.
<div id="MainPage" data-role="page" >
<div data-role="content">
RENAME
</div>
</div>
<div id="ViewPage" data-role="page" >
<div data-role="content">
<ul id="viewlist" data-role="listview" data-filter="true" data-filter-placeholder="Sample Contents" data-inset="true">
<!-- array contents goes here -->
</ul>
</div>
</div>
<script>
var sampleContent = new Array( );
displayArray( )
{
for(var scan=0; scan<sampleContent.length; detect++)
{
$('#viewlist').append('<li>' + sampleContent[scan] + '</li>');
}
}
</script>
My code works during the first press of the button but when i pressed it the second time, the list view becomes messed up.
Any help or advice will be glady accepted thanks in advance.
edited, i have figured out how to do it but I am having problems during the second press of the button.
First of i don't want to be rude but i think you should start first to read some basics about android.
Like:
Android Activities , life cycle of activities
Layout in Android (how to add button on an activity then respond to a click etc) , different existing Layout in android and android widget (like the listView for example)
of course there are a lot more to read but it s a good way to start.
However i will provide you codes that will do what you are asking for and i will try to explain as much as i can
First of all you need to create the other activity and inside the layout of that activity insert a listview
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
then the java code of the other activity will look like this
public class MainActivity2 extends Activity {
//create the array adapter that will input your array and convert it into a list of view
ArrayAdapter<String> arrayAdapter;
String[] list;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = (ListView)findViewById(R.id.listView);
// get the array from ressource and insert them on an array
list = getResources().getStringArray(R.array.listArray);
// then create the arrayadpater with input your array of string if you dont get //anything here just read android documentation about arrayAdapter
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list );
//then set the adapter to your listView
listView.setAdapter(arrayAdapter);
}
}
res xml file
<resources>
<string-array name="listArray">
<item>Apple</item>
<item>Banana</item>
<item>Cherry</item>
<item>Cranberry</item>
<item>Grape</item>
<item>Grape</item>
</string-array>
</resources
>
Hope it will help
Just add .listview('refresh') after you have added all items.
$('#viewlist').listview('refresh');
If you want to empty the list each time and refill it, call .empty() before the for loop:
$('#viewlist').empty();
To use better jquery mobile coding, structure your code like this:
Take the onclick out of the anchor tag and add an id:
<a id="viewPageBtn" href="#ViewPage" data-role="button" >RENAME</a>
In your script tag, handle pagecreate on the main page, and within it handle the click event of the anchor:
$(document).on("pagecreate", "#MainPage", function(){
var sampleContent = ["item 1", "item 2", "item 3"];
$("#viewPageBtn").on("click", function(){
$('#viewlist').empty();
for(var scan=0; scan < sampleContent.length; scan++)
{
$('#viewlist').append('<li>' + sampleContent[scan] + '</li>').listview('refresh');
}
$('#viewlist').listview('refresh');
});
});

How to dynamically assign displayName in lineseries - tooltip & displayName shows [object Object]

This flashbuilder 4.6 application displays a single lineSeries on a line chart based on a chosen name from a dropdown list and the lineseries data shows fine. However, the tooltip shows [object Playername_returntype] instead of the dropdown chosen name.
I also want to dynamically assign the same chosen name from the dropdown to the displayName of the lineseries but have been unable to achieve this. Result shows [object Playername_returntype] as in the tooltip.
Any help would be much appreciated.
Thanks.
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:pool_ratings_yr1service="services.pool_ratings_yr1service.*"
xmlns:pool_playerservice="services.pool_playerservice.*"
minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
protected function linechart1_creationCompleteHandler(event:FlexEvent):void
{
getpool_ratings_yr1Result.token = pool_ratings_yr1Service.getpool_ratings_yr1('Greenleaf');
}
protected function dropDownList_creationCompleteHandler(event:FlexEvent):void
{
getAllpool_playerResult.token = pool_playerService.getAllpool_player();
}
private function comboBoxChange():void
{
var selectedName:String = dropDownList.selectedItem.lname;
getpool_ratings_yr1Result.token = pool_ratings_yr1Service.getpool_ratings_yr1(selectedName);
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="getpool_ratings_yr1Result"/>
<pool_ratings_yr1service:Pool_ratings_yr1Service id="pool_ratings_yr1Service"
fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
showBusyCursor="true"/>
<s:CallResponder id="getAllpool_ratings_yr1Result"/>
<s:CallResponder id="getAllpool_playerResult"/>
<pool_playerservice:Pool_playerService id="pool_playerService"
fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
showBusyCursor="true"/>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:LineChart id="linechart1" x="151" y="88" width="800"
creationComplete="linechart1_creationCompleteHandler(event)"
dataProvider="{getpool_ratings_yr1Result.lastResult}" showDataTips="true">
<mx:verticalAxis>
<mx:LinearAxis id="v1" minimum="2000" maximum="2500" title="Average Elo Rating" />
</mx:verticalAxis>
<mx:series>
<mx:LineSeries id="lineSeries" displayName="{dropDownList.selectedItem}" yField="avg_rating"/>
</mx:series>
<mx:horizontalAxis>
<mx:CategoryAxis id="categoryAxis" categoryField="yr"/>
</mx:horizontalAxis>
</mx:LineChart>
<mx:Legend dataProvider="{linechart1}"/>
<s:DropDownList id="dropDownList" x="10" y="88"
creationComplete="dropDownList_creationCompleteHandler(event)"
labelField="lname"
change="comboBoxChange()">
<s:AsyncListView list="{getAllpool_playerResult.lastResult}"/>
</s:DropDownList>
Not entirely sure what you're after, but for the LineSeries displayName, here's what I did:
Created a [Bindable] String variable for each Line Series, and then whenever the data changed, just assign whatever value you want to the variable. In your example:
private function comboBoxChange():void
{
var selectedName:String = dropDownList.selectedItem.lname;
getpool_ratings_yr1Result.token = pool_ratings_yr1Service.getpool_ratings_yr1(selectedName);
// Private Bindable String variable(myString) assigned earlier
myString = selectedName;
}
And then in your mxml:
displayName="{myString}"
Hope that's clear enough!

Can I put a Button in a RichTextBox in Silverlight 4?

I would like to allow users to put a System.Windows.Controls.Button in the System.Windows.Controls.RichTextBox. The button would do a pre-defined thing.
I figured out how to do this. It's called an InlineUIContainer you can do something like this to get it working. Although it doesn't save it into the Xaml
var p = new Paragraph();
var inlineUIContainer = new InlineUIContainer() { Child = new Button() { Content = "This is a Button!" } };
p.Inlines.Add(inlineUIContainer);
_richTextBox.Blocks.Add(p);

Resources