1 // Copyright (c) Microsoft Corporation. All Rights Reserved.
5 using System
.Windows
.Controls
;
6 using System
.Windows
.Data
;
7 using System
.Windows
.Documents
;
8 using System
.Windows
.Media
;
9 using System
.Windows
.Shapes
;
11 using Microsoft
.ServiceModel
.Samples
;
12 using System
.Windows
.Markup
;
14 using System
.Windows
.Threading
;
16 namespace ConsumingXamlFromWpf
18 public partial class Window1
: Window
22 InitializeComponent();
25 protected override void OnInitialized(EventArgs e
)
27 base.OnInitialized(e
);
29 // Handle button click
30 _getFruitButton
.Click
+= _getFruitButton_Click
;
33 // Keep proxy for lifetime of Window
34 ProvideFruitClient client
= new ProvideFruitClient();
36 protected override void OnClosed(EventArgs e
)
40 // Clean up proxy when Window closes
44 void _getFruitButton_Click(object sender
, RoutedEventArgs e
)
46 // Get the four fruit asynchronously, calling GotFruit when
47 // each piece is available and passing the type of the fruit
48 // via IAsyncResult.AsyncState so we can label each piece of fruit
49 client
.BeginGetFruit(FruitType
.Banana
, GotFruit
, FruitType
.Banana
);
50 client
.BeginGetFruit(FruitType
.Cherry
, GotFruit
, FruitType
.Cherry
);
51 client
.BeginGetFruit(FruitType
.Lemon
, GotFruit
, FruitType
.Lemon
);
52 client
.BeginGetFruit(FruitType
.Lime
, GotFruit
, FruitType
.Lime
);
55 // Show the result when call to web service completes
56 void GotFruit(IAsyncResult ar
)
58 //This call back is not coming on the ui thread, so we must use the
59 //Dispatcher to invoke on the ui thread
60 Dispatcher
.Invoke(DispatcherPriority
.Normal
, new AsyncCallback(AddFruit
), ar
);
63 void AddFruit(IAsyncResult ar
)
65 // Harvest the results
66 FruitType fruitType
= (FruitType
)ar
.AsyncState
;
67 string fruitXaml
= client
.EndGetFruit(ar
);
69 // Parse the XAML data
70 FrameworkElement graphic
= null;
71 using (StringReader sreader
= new StringReader(fruitXaml
))
72 using (XmlTextReader xreader
= new XmlTextReader(sreader
))
74 graphic
= (FrameworkElement
)XamlReader
.Load(xreader
);
75 graphic
.LayoutTransform
= new ScaleTransform(0.25, 0.25);
78 // Compose some text and the graphic
79 TextBlock text
= new TextBlock();
80 text
.Text
= fruitType
.ToString() + ": ";
82 StackPanel fruitPanel
= new StackPanel();
83 fruitPanel
.Orientation
= Orientation
.Horizontal
;
84 fruitPanel
.Children
.Add(text
);
85 fruitPanel
.Children
.Add(graphic
);
87 // Show the text and the graphic
89 _stackPanel
.Children
.Add(fruitPanel
);