|
using System;<br />
using System.Text;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Xml.Linq;<br />
using System.Xml;<br />
using System.Net;<br />
using System.Windows;<br />
using System.Windows.Controls;<br />
using System.Windows.Documents;<br />
using System.Windows.Input;<br />
using System.Windows.Media;<br />
using System.Windows.Media.Animation;<br />
using System.Windows.Shapes;<br />
using System.IO;<br />
<br />
namespace LinqToXmlSilverlight090322<br />
{<br />
public partial class Page : UserControl<br />
{<br />
public Page()<br />
{<br />
InitializeComponent();<br />
<br />
StringBuilder sb = new StringBuilder("test.xml", 10);<br />
XmlWriterSettings xws = new XmlWriterSettings();<br />
xws.OmitXmlDeclaration = true;<br />
xws.Indent = true;<br />
<br />
using (XmlWriter xw = XmlWriter.Create(sb, xws))<br />
{<br />
<br />
XDocument xd = new XDocument(<br />
new XElement("MyFractals",<br />
new XElement("first",<br />
new XAttribute("range", "1,5"),<br />
new XAttribute("startRe", "1,0"),<br />
new XAttribute("startIm", "-0,7"),<br />
new XAttribute("startColor", "100")<br />
),<br />
new XElement("second",<br />
new XAttribute("range", "0,5"),<br />
new XAttribute("startRe", "0,105"),<br />
new XAttribute("startIm", "-0,6"),<br />
new XAttribute("startColor", "160")<br />
),<br />
new XElement("third",<br />
new XAttribute("range", "0,75"),<br />
new XAttribute("startRe", "0,5"),<br />
new XAttribute("startIm", "0,204"),<br />
new XAttribute("startColor", "60")<br />
)<br />
)<br />
);<br />
<br />
xd.Save(xw);<br />
}<br />
<br />
using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString())))<br />
{<br />
XDocument xd2 = XDocument.Load(reader);<br />
XElement rt = xd2.Element(XName.Get("MyFractals"));<br />
var xyz = from e in rt.Elements()<br />
select e;<br />
}<br />
}<br />
}<br />
}
Why doesn't this work? There is no test.xml file created and when the program tries to open the xml file (which I manually placed) I get an error message saying 'line 1 character 1 illegal character'.
Is there anybody who could tell me how to get this to work?
Thanks,
Ranger.
|
|
|
|
|
Ranger49 wrote: There is no test.xml file created
The XmlWriter you are using should be writing the XML to the StringBuilder,
not to a file.
In Silverlight 2, the only access you have to the user's local hard disk
is using Isolated Storage:
How to: Save XMLWriter Content to Isolated Storage[^]
Ranger49 wrote: I get an error message saying 'line 1 character 1 illegal character'.
Probably because your StringBuilder has "test.xml" at the beginning of its contents,
which isn't valid XML.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Ok, I see...
What I would want to do is write an XML file with data to the server where the Silverlight application is situated.
Is this possible at all? Or is the only option Isolated Storage on the computer of the person who is viewing the Silverlight application?
Thanks!
Ranger.
|
|
|
|
|
Silverlight runs entirely on the client side.
To write to a server, you'll need to send it to the server.
Here's some info on ways to do that:
Silverlight Networking and Communication[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks...
I will study this.
Ranger
|
|
|
|
|
Thanks ABitSmart And Pete for your answers. I am trying to use RoutedEvent correctly this time. But the RoutingStrategy doesn't work.
MyButtonSimple class has a RoutedEvent which RoutingStrategy is Tunnel.
public class MyButtonSimple: Button
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
"Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));
// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
}
public MyButtonSimple()
{
this.Tap += new RoutedEventHandler(MyButtonSimple_Tap);
}
//Handle the TapEvent
void MyButtonSimple_Tap(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Tag:"+this.Tag.ToString());
}
}
Then I have a Window1 to test MyButtonSimple like this
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Width="300"
Height="300">
<local:MyButtonSimple Width="80"
Height="80"
Tag="OuterButton">
<Canvas Width="60"
Height="60"
PreviewMouseDown="Canvas_PreviewMouseDown">
<local:MyButtonSimple Width="20"
Height="20"
Tag="InnerButton"/>
</Canvas>
</local:MyButtonSimple>
</Window>
private void Canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TagEvent);
(sender as Canvas).RaiseEvent(newEventArgs);
}
Well, just as my anticipation, the output window printed "OutterButton". But when I change the RoutingStrategy to RoutingStrategy.Tunnel, which I anticipate to raise the InnerButton ,the output window still printed "OutterButton".
Is it against the routed mechanism? And Many ways I've tried since then(such as raise the event in Canvas_MouseDown), it still printed "OutterButton". And what I wanna see is "InnerButton".
Am I doing wrong angain or the RoutingStrategy doesn't work this way?
|
|
|
|
|
mildred-frontfree wrote: But the RoutingStrategy doesn't work.
No, it works.
When you set it as bubbling, it starts from itself and ends at the root.
When you set it as Tunneling, it starts from the root and ends at itself.
mildred-frontfree wrote: (sender as Canvas).RaiseEvent(newEventArgs);
Remember, you are raising the event from the Canvas.
In your case, when you set it as bubble, it started from Canvas and searched upto the root. It only found OuterButton, which is correct.
When you set it tunneling, it started from the root. It found Outerbutton fired event for it. Continued upto the Canvas and never found anymore MyButtonSimple.
If instead you had raised it from InnerButton, you would have got your desired output.
Try this,
<local:MyButtonSimple Width="100" Height="100" Tag="OuterButton">
<Canvas Width="80" Height="80">
<local:MyButtonSimple Width="60" Height="60" Tag="InnerButton">
<Canvas Width="40" Height="40" PreviewMouseDown="Canvas_PreviewMouseDown">
<local:MyButtonSimple Width="20" Height="20" Tag="MoreInnerButton"/>
</Canvas>
</local:MyButtonSimple>
</Canvas>
</local:MyButtonSimple>
When you set the event as Bubbling, it will print InnerButton and then OuterButton.
When you set the event as Tunneling, it will print OuterButton and then InnerButton.
|
|
|
|
|
Yes, Indeed, it works very well. I misunderstand the Tunneling mechanism, I thought it route down to mySimpleButton form the OriginalSource not the Root Element. RoutedEvent is really flexible. Thanks for your suggestion!
|
|
|
|
|
Hi,
I'm using Silverlight.createObject() and createFromXaml() Javascript functions to render some XAML files. This works great but the Button element not. I can make some TextBlock, TextBox, Grid, StackPanel, and more controls, but the simple Button I can't be added to my dynamic XAML. Someone could explain me this?
My code is something like this:
<pre>
Javascript
function embedSilverlight(parentElement, pluginId, userContext)
{
return Silverlight.createObject("sample.xaml",
parentElement, pluginId,
{
width: 500, background: "white", alt: 'No Silverlight',
version:"2.0.31005.0", autoUpgrade:true
},
{
onError: onSLError,
onLoad: function(plugIn, userContext, sender)
{
var xamlFragment = $(parentElement.id + '_xaml').get('html');
xamlFragment = xamlFragment.substr(12, xamlFragment.length - 18);
var child = plugIn.content.createFromXaml(xamlFragment, false);
sender.children.add(child);
}
},
"", userContext);
}
function onSLError(sender, args)
{
// Display error message.
}
sample.xaml
<canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="slView">
</canvas>
And the content are snippets like:
<grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" width="500"><grid.rowdefinitions><rowdefinition height="20" /><rowdefinition height="*" /><rowdefinition height="20" /></grid.rowdefinitions><textblock grid.row="0">Form Silverlight</textblock><stackpanel grid.row="1"><grid margin="4, 4, 4, 4"><grid.columndefinitions><columndefinition width="100" /><columndefinition width="*" /><columndefinition width="50" /></grid.columndefinitions><textblock grid.column="0">Name</textblock><textbox grid.column="1" width="200" text="Simio" /><textblock grid.column="2">*</textblock></grid><grid margin="4, 4, 4, 4"><grid.columndefinitions><columndefinition width="100" /><columndefinition width="*" /><columndefinition width="50" /></grid.columndefinitions><textblock grid.column="0">Email</textblock><textbox grid.column="1" width="200" text="" /><textblock grid.column="2">*</textblock></grid></stackpanel><stackpanel orientation="Horizontal" background="Silver" grid.row="2"><rectangle stroke="White" strokethickness="1" /><textblock>Sign up</textblock></stackpanel></grid>
</pre>
----
hxxbin
|
|
|
|
|
What am I missing?
I don't see anything related to a button control in your post...
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yes, sorry, but the point is when I try to add a child Button in my canvas, I can't do it. It returns me a Javascript error saying the Button element is unknown.
----
hxxbin
|
|
|
|
|
I am using .Net 3.5 and WPF.
I've got a list box that needs to draw text in one of three ways, depending on some criteria. In the process of trying to make this work (which it still doesn't), I've got this question:
When I do this:
private void listboxWords_LayoutUpdated(object sender, EventArgs e)
{
ListBox listbox = sender as ListBox;
}
...both of the method parameters are null. Why?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
The way to owner draw is to have a number of derived classes for styles and then to set the style in XAML. As for your event, I have no idea, but I do believe that's the wrong time to be trying to change how things look.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Well, I googled the helll out of it before posting my question, and that's how everyone seems to be doing it. I hate WPF.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
LayoutUpdated is a static event. It is fired on layout change of any element in the element tree. It's by design that no parameters are passed to it. That's how it is. Link[^]
For your case, you might want you have a trigger (if the criteria is a property value) or an ItemTemplateSelector[^] (if the criteria is a type)
|
|
|
|
|
ABitSmart wrote: It's by design that no parameters are passed to it.
Then why the frak does the method have parameters? (just a rhetorical question meant to indicate a certain level of anger and frustration - you don't need to answer it).
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John - why not use something like this[^]? It's simple and much more effective.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys
|
|
|
|
|
Pardon my abruptness, but I've pretty much reached the end of my rope where WPF is concerned, so...
-------------
Because I didn't frakking know about templates, and there's really nothing I can find that presents the information COMPLETELY. All of the examples I've found assume I want to put frakking pictures in a listbox. I simply have strings thatI want to apply visual styles to based on certain criteria that I establish.
In WinForms, this took about 15 lines of code inside an event handler, and took me 20 minutes to figure out how to do. For WPF, it appears I have to write HUNDREDS of lines of code/XAML to do the same frakking thing, and I'v been trying to find a way to do it for TWO DAYS now! How the frak is this "better" than WinForms? Half-assed tools, requires twice the work on my part, and no frakking support from the anal pores that dreamed this steaming pile of horseshit up in the first place.
All I see is a bunch of half-brained ass-kissers running around with little WPF erections because they got it in their heads that this is somehow the end-all be-all of coding.
-------------
...even after that rant, I don't feel the slightest bit better. I'm just thoroughly pissed off.
I'm now off to look at the link you provided, but I'm really not confident that it will answer my questions nor provide even half of a solution.
I'm trying to convert my anagrams article to WPF. The Listbox is not bound to a collection. It accepts manually entered strings which are displayed in black (the default control color). When the user clicks a button, the strings that were already in the listbox are displayed in red, and all other possible strings are added and displayed in dark gray italics.
I'm not looking someone to code this for me - I just want to know how it's supposed to be done.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Well, I'm glad I gave you the chance to vent. When I started with WPF, I had pretty much the same reaction as you - it's taken me a long time to get to the point where it's second nature to me, and I know that it can be a pain.
Thing is though - if you stick with it, parts do fall into place - but you have to want to work with WPF in order to put that time in. If you don't want to work with it, don't - there are plenty of alternatives that don't require you to go through the "pain barrier".
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys
|
|
|
|
|
I guess the worst part is that I'm alone in my dissatisfaction with WPF here at work. Every time I run into something like this, it just pisses me off even more that we're even using WPF.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John - here's a simple application that you can use to test this type of functionality. I've added an ObservableCollection which consists of words that the user adds in via a textbox.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows;
namespace SampleWpfApplication
{
public class WordManager
{
public string Text { get; set; }
}
public class Words : ObservableCollection<WordManager>
{
}
public class WordTemplateSelector : DataTemplateSelector
{
public DataTemplate NormalWordTemplate { get; set; }
public DataTemplate RedWordTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
WordManager word = item as WordManager;
if (word.Text == "Howdy")
{
return RedWordTemplate;
}
else
{
return NormalWordTemplate;
}
}
}
} Here's the XAML that's used:
<Window x:Class="SampleWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWpfApplication"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="NormalWords">
<Grid>
<TextBlock Text="{Binding Path=Text}" Foreground="Brown"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="RedWords">
<TextBlock Text="{Binding Path=Text}" FontWeight="Bold" Foreground="Red"/>
</DataTemplate>
<local:Words x:Key="Words" />
<local:WordTemplateSelector
x:Key="TemplateSelector"
NormalWordTemplate="{StaticResource NormalWords}"
RedWordTemplate="{StaticResource RedWords}" />
</Window.Resources>
<StackPanel>
<ListBox x:Name="myList"
ItemsSource="{Binding Mode=Default, Source={StaticResource Words}}"
ItemTemplateSelector="{StaticResource TemplateSelector}"
x:FieldModifier="private"></ListBox>
<TextBox x:Name="myText" x:FieldModifier="private" Width="30" />
<Button Content="Click" Click="Button_Click" />
</StackPanel>
</Window> Finally, here's the button click code:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(myText.Text))
{
Words words = this.Resources["Words"] as Words;
if (words != null)
{
words.Add(new WordManager{Text=myText.Text});
}
}
} Basically the XAML is set up so that there can be multiple templates, which get applied based on certain conditions - chosen by the WordTemplateSelector class. By providing a template selector class, it's possible to add as many conditions as you like - all of which can have a simple template. The beauty of the approach here, is that we're using an ObservableCollection so that the template gets reevaluated whenever an item is added in. If you need more information, please feel free to ask.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys
|
|
|
|
|
Well, it doesn't like part of the xaml stuff (I had implemented all of the stuff you showed before I saw your message). Here's what I have:
namespace AnagramsWPF
{
public class WordItem
{
public string Text { get; set; }
public bool Used { get; set; }
public WordItem(string text)
{
Text = text;
Used = false;
}
public override string ToString()
{
return Text;
}
}
public class WordTypeTemplateSelector : DataTemplateSelector
{
public DataTemplate UnusedWordTemplate { get; set; }
public DataTemplate UsedWordTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
WordItem wordItem = item as WordItem;
DataTemplate template = null;
if (wordItem.Used)
{
template = UsedWordTemplate;
}
else
{
template = UnusedWordTemplate;
}
return template;
}
}
}
In the xaml, I have this:
<Window.Resources>
<DataTemplate x:Key="unusedWord">
<Grid>
<TextBlock Text="{Binding Path=Text}" Foreground="DarkGray" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="usedWord">
<Grid>
<TextBlock Text="{Binding Path=Texe}" Foreground="Red" FontStyle="Italic" />
</Grid>
</DataTemplate>
<local:WordTypeTemplateSelector
x:Key="wordTypeTemplateSelector"
UsedWordTemplate="{StaticResource usedwordTemplate}"
UnusedWordTemplate="{StaticResource unusedwordTemplate}" />
</Window.Resources>
It doesn't like the underlined portion (it shows the error - "The type 'local:WordTypeTemplateSelector' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.").
I don't need the ObservableCollection stuff because the words are already in the collection before the user starts putting words into the listbox.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
modified on Tuesday, March 24, 2009 4:15 PM
|
|
|
|
|
You need to add this into your XAML to use the WordTypeTemplateSelector:
xmlns:local="clr-namespace:AnagramsWPF" If it's already present, build your application - this type of reference only works if there are no build errors that prevent that class being updated. I'm busy writing a WPFified version of CPAM and this might help to clarify things for you.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys
|
|
|
|
|
Well, I got it to work, but it was a royal pain in the ass. It was a LOT simpler in WinForms.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Congratulations.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys
|
|
|
|
|