<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Biggle&#039;s Blog &#187; HowTo</title>
	<atom:link href="http://www.biggle.de/blog/tag/howto/feed" rel="self" type="application/rss+xml" />
	<link>http://www.biggle.de/blog</link>
	<description>Web- und Software Development</description>
	<lastBuildDate>Tue, 07 Feb 2012 13:08:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Biggle ist kein Palindrom</title>
		<link>http://www.biggle.de/blog/biggle-ist-kein-palindrom</link>
		<comments>http://www.biggle.de/blog/biggle-ist-kein-palindrom#comments</comments>
		<pubDate>Fri, 03 Dec 2010 10:44:32 +0000</pubDate>
		<dc:creator>Mario Priebe</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.biggle.de/blog/?p=6044</guid>
		<description><![CDATA[Eine kleine Übungsaufgabe für mich, die es zu lösen gab. Es sollte heraus gefunden werden, ob es sich bei einem String um ein Palindrom handelt. Mein Lösungsansatz lautet wie folgt: Ich befreie zunächst die Buchstaben von Sonder- und Leerzeichen und konvertiere alle Zeichen zu Kleinbuchstaben. Dann durchlaufe ich eine for-Schleife mit eine Länge des Stringparameters. [...]]]></description>
			<content:encoded><![CDATA[<p>Eine kleine Übungsaufgabe für mich, die es zu lösen gab. Es sollte heraus gefunden werden, ob es sich bei einem String um ein <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RlLndpa2lwZWRpYS5vcmcvd2lraS9QYWxpbmRyb20=">Palindrom</a> handelt.</p>
<p>Mein Lösungsansatz lautet wie folgt:<br />
 Ich befreie zunächst die Buchstaben von Sonder- und Leerzeichen und konvertiere alle Zeichen zu Kleinbuchstaben. Dann durchlaufe ich eine for-Schleife mit eine Länge des Stringparameters.</p>
<p>Ich zerlege den String in ein CharArray und vergleiche innerhalb der Schleife, den vorderen mit den letzten Buchstaben. Sind diese gleich wird der nächste vordere und der nächste hintere Buchstabe verglichen. Das geht so lange bis diese sich in der Mitte treffen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">class</span> ExtensionMethods
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> static bool IsPalindrome<span style="color: #009900;">&#40;</span>this string value<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//von Leer- und Sonderzeichen befreien</span>
        value <span style="color: #339933;">=</span> value<span style="color: #339933;">.</span>ToLower<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>RemoveSpecialCharacters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        int minValue <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        int maxValue <span style="color: #339933;">=</span> value<span style="color: #339933;">.</span>Length <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> charArray <span style="color: #339933;">=</span> value<span style="color: #339933;">.</span>ToCharArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>int i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> value<span style="color: #339933;">.</span>Length <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>charArray<span style="color: #009900;">&#91;</span>minValue<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> charArray<span style="color: #009900;">&#91;</span>maxValue<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                minValue<span style="color: #339933;">++;</span>
                maxValue<span style="color: #339933;">--;</span>
                <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static string RemoveSpecialCharacters<span style="color: #009900;">&#40;</span>this string value<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> Regex<span style="color: #339933;">.</span>Replace<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> <span style="color: #339933;">@</span><span style="color: #0000ff;">&quot;[^a-zA-Z0-9]&quot;</span><span style="color: #339933;">,</span> string<span style="color: #339933;">.</span><span style="color: #990000;">Empty</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Verwendung:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Program
<span style="color: #009900;">&#123;</span>
    static void Main<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        PalindromFactory factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PalindromFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> palindrome <span style="color: #339933;">=</span> factory<span style="color: #339933;">.</span>GetPalindrome<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        string output <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;{0} ist {1}ein Palindrom&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> palindrom in palindrome<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span>String<span style="color: #339933;">.</span>Format<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> palindrom<span style="color: #339933;">,</span> palindrom<span style="color: #339933;">.</span>IsPalindrome<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;k&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> palindromSaetze <span style="color: #339933;">=</span> factory<span style="color: #339933;">.</span>GetPalindromSaetze<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> palindrom in palindromSaetze<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span>String<span style="color: #339933;">.</span>Format<span style="color: #009900;">&#40;</span>output<span style="color: #339933;">,</span> palindrom<span style="color: #339933;">,</span> palindrom<span style="color: #339933;">.</span>IsPalindrome<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;k&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ich hoffe ich habe nichts vergessen, getestet jedoch habe ich diese Methode mit der <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RlLndpa2lwZWRpYS5vcmcvd2lraS9MaXN0ZV9kZXV0c2NoZXJfUGFsaW5kcm9tZQ==">Liste deutscher Palindrome</a> aus Wikipedia. Nicht-Palindrome natürlich mit einbegriffen : )</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/// Stellt eine Sammlung von Palindrome und Palindromsätze bereit</span>
<span style="color: #666666; font-style: italic;">/// Quelle Wikipedia</span>
<span style="color: #666666; font-style: italic;">/// http://de.wikipedia.org/wiki/Liste_deutscher_Palindrome</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PalindromFactory
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> Palindrome <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> GetPalindrome<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Palindrome <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">&quot;Aha&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Amokoma&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Amoralaroma&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Anina&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Anna&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Annasusanna&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Aua&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Bub&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Burggrub&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Biggle&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//kein Palindrom</span>
            <span style="color: #0000ff;">&quot;Mario&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//kein Palindrom</span>
            <span style="color: #0000ff;">&quot;Mongognom&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Nebelleben&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;neben&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Neffen&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;nennen&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;netten&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;netzten&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Neozoen&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;nun&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Omo&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Otto&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Priebe&quot;</span> <span style="color: #666666; font-style: italic;">//kein Palindrom</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> Palindrome<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> GetPalindromSaetze<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Palindrome <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">&quot;Die Liebe fleht: Helfe bei Leid!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Die Liebe geht, hege Beileid!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Die Liebe, ist sie Beileid?&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Die Liebe ist Sieger, rege ist sie bei Leid.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Die Liebe ist Sieger, stets rege ist sie bei Leid.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Die Niere bot Komik: nass sank im Oktober ein Eid.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Die Rede — ist sie der Eid?&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Dreh Magiezettel um, Amulette zeig am Herd!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Du, erfror Freud?&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Der Fred&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eine güldne, gute Tugend: Lüge nie!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eine Horde bedrohe nie!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eine Hure ruhe nie.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eine Note betone nie.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eine so Kesse kose nie.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eine treue Familie bei Lima feuerte nie.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Einhorn roh? Nie!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eins nutzt uns: Amore. Die Rederei da, die Rederei der Omas nutzt uns nie.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Eis feil! Ei, wo Eis feil lief sie, o wie lief sie.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Elietta hat Teile.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Ella rüffelte Detlef für alle.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Elly biss Sibylle.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Emma, behend 'ne Hebamme!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Emma, so litt Tilos Amme!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Emmas Amme&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Er habe nie eine Bahre.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Erhabene Bahre&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Erhöre nie eine Röhre.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Erika feuert nur untreue Fakire.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Erol, red nie in der Lore.&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;Alles hat seine Zeit, nur die alten Weiber nicht.&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//Kein Palindromsatz</span>
            <span style="color: #0000ff;">&quot;Es eilt, immer ahnend Nebel, reger der Flegel Fred, reg' erlebend nen Harem mit Liese.&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> Palindrome<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Für einen schnellen Verwendungszeck (<strong>weil man das auch so oft braucht oO</strong>)  habe ich das ganze als ExtensionMethod implementiert.</p>
<p>Wie schaut&#8217;s aus, hast du Verbesserungsvorschläge? Würdest du es anders machen, wenn ja wie? Du kannst im Kommentarfeld mit [code]<strong>var* </strong>deinCodeSnippet = 0;[/code] posten.</p>
<p><br class="spacer_" /></p>
<p>Viel Spaß beim entwickeln : )</p>
<p><br class="spacer_" /></p>
<p>* = <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21lbnR6ZWwubmFtZS8yMDEwLzEyLzAyL3ZhcnVtLw==">Var oder nicht var&#8230;</a></p>
<hr /><p style="float:right; font-size:0.9em;">Dieser Beitrag stammt von <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGU=">Mario Priebe</a>.</p> <img src="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=6044" width="1" height="1" style="display: none;" /><h2  class="related_post_title">Ähnliche Beiträge</h2><ul class="related_post"><li>9. Januar 2011 -- <a href="http://www.biggle.de/blog/mehrfachvererbung-ein-beispiel-in-c" title="Mehrfachvererbung – Ein Beispiel in C#">Mehrfachvererbung – Ein Beispiel in C#</a></li><li>18. November 2009 -- <a href="http://www.biggle.de/blog/php-entwicklungsumgebung-auf-windows" title="PHP Entwicklungsumgebung unter Windows">PHP Entwicklungsumgebung unter Windows</a></li><li>20. April 2011 -- <a href="http://www.biggle.de/blog/basta-on-tour-%e2%80%93-architecture-best-practices-gewinnspiel" title="BASTA! on Tour – Architecture &#038; Best Practices + Gewinnspiel">BASTA! on Tour – Architecture &#038; Best Practices + Gewinnspiel</a></li><li>7. Februar 2011 -- <a href="http://www.biggle.de/blog/sqlite-datenbanken-in-csharp-applikationen-verwenden" title="SQLite Datenbanken in C# Applikationen verwenden">SQLite Datenbanken in C# Applikationen verwenden</a></li><li>7. Februar 2011 -- <a href="http://www.biggle.de/blog/c-programmieren-fuer-anfaenger" title="C Programmieren für Anfänger">C Programmieren für Anfänger</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.biggle.de/blog/biggle-ist-kein-palindrom/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Windows Phone 7 – Push Notification</title>
		<link>http://www.biggle.de/blog/windows-phone-7-push-notification-implementierung-beispiel</link>
		<comments>http://www.biggle.de/blog/windows-phone-7-push-notification-implementierung-beispiel#comments</comments>
		<pubDate>Tue, 16 Nov 2010 15:17:03 +0000</pubDate>
		<dc:creator>Mario Priebe</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://www.biggle.de/blog/?p=5850</guid>
		<description><![CDATA[Push Notifications erstellen einen Nachrichtenkanal zwischen dem Microsoft Push Notification (MPN) Service und einem Push Client wie z.B. das Windows Phone 7. Ein MPN Service kann einem Client, welcher diesen externen Service abonniert hat, Nachrichten schicken ohne das dieser ständig nach Nachrichten triggern, oder eine Verbindung aufrecht erhalten muss. Einen Überblick der Funktionsweise bekommt man [...]]]></description>
			<content:encoded><![CDATA[<p>Push Notifications erstellen einen Nachrichtenkanal zwischen dem Microsoft Push Notification (MPN) Service und einem Push Client wie z.B. das Windows Phone 7. Ein MPN Service kann einem Client, welcher diesen externen Service abonniert hat, Nachrichten schicken ohne das dieser ständig nach Nachrichten triggern, oder eine Verbindung aufrecht erhalten muss. Einen Überblick der Funktionsweise bekommt man im <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21zZG4ubWljcm9zb2Z0LmNvbS9lbi11cy9saWJyYXJ5L2ZmNDAyNTU4KFZTLjkyKS5hc3B4">MSDN</a>.</p>
<p>Ich hab mich mal ran gemacht und eine Applikation erstellt, die  diesen Service konsumiert. Die Beispielapplikation besteht aus einen WCF Service, einer WPF Applikation und einer Windows Phone 7 Applikation. Die WPF Applikation sendet eine Nachricht an den WCF Service und die WP7 Applikation abonniert eine Push Notification um diese Nachricht zu erhalten.</p>
<h2>Projektstruktur</h2>
<p>Dazu erstellen wir uns eine Solution mit  einem <strong>Windows-Phone Projekt</strong> (WindowsPhonePushNotification), fügen eine <strong>WCF Dienstbibliothek</strong> (WindowsPhonePushNotificationService)  und eine <strong>WPF-Anwendung</strong> (WindowsPhonePushNotificationClient) hinzu.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZV8xMS5wbmc="><img class="alignnone size-full wp-image-5853" title="PushNotificationService_1" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService_11.png" alt="" width="295" height="544" /></a></p>
<h2>Der WCF Service</h2>
<p>Der Contract besteht aus zwei Methoden. Subscribe und SendToast:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>ServiceContract<span style="color: #009900;">&#93;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IService1
<span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#91;</span>OperationContract<span style="color: #009900;">&#93;</span>
    void Subscribe<span style="color: #009900;">&#40;</span>string url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>OperationContract<span style="color: #009900;">&#93;</span>
    void SendToast<span style="color: #009900;">&#40;</span>string title<span style="color: #339933;">,</span> string message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Die Implementierung des Contracts sieht wie folgt aus:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">private static Uri _uri;
public void Subscribe(string url)
{
    _uri = new Uri(url);
}
&nbsp;
public void SendToast(string title, string message)
{
    string toastMsg = &quot;<span style="color: #000000; font-weight: bold;">&lt;?</span>xml version<span style="color: #339933;">=</span>\<span style="color: #0000ff;">&quot;1.0<span style="color: #000099; font-weight: bold;">\&quot;</span> encoding=<span style="color: #000099; font-weight: bold;">\&quot;</span>utf-8<span style="color: #000099; font-weight: bold;">\&quot;</span>?&gt;&quot;</span> <span style="color: #339933;">+</span>
        <span style="color: #0000ff;">&quot;&lt;wp:Notification xmlns:wp=<span style="color: #000099; font-weight: bold;">\&quot;</span>WPNotification<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;&lt;wp:Toast&gt;&quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #0000ff;">&quot;&lt;wp:Text1&gt;&quot;</span> <span style="color: #339933;">+</span> title <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&lt;/wp:Text1&gt;&quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #0000ff;">&quot;&lt;wp:Text2&gt;&quot;</span> <span style="color: #339933;">+</span> message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&lt;/wp:Text2&gt;&quot;</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;&lt;/wp:Toast&gt; &quot;</span> <span style="color: #339933;">+</span>
        <span style="color: #0000ff;">&quot;&lt;/wp:Notification&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> messageBytes <span style="color: #339933;">=</span> <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Text<span style="color: #339933;">.</span>Encoding<span style="color: #339933;">.</span>UTF8<span style="color: #339933;">.</span>GetBytes<span style="color: #009900;">&#40;</span>toastMsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    SendMessage<span style="color: #009900;">&#40;</span>_uri<span style="color: #339933;">,</span> messageBytes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> static void SendMessage<span style="color: #009900;">&#40;</span>Uri uri<span style="color: #339933;">,</span> byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> message<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    HttpWebRequest request <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpWebRequest<span style="color: #009900;">&#41;</span>WebRequest<span style="color: #339933;">.</span>Create<span style="color: #009900;">&#40;</span>uri<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    request<span style="color: #339933;">.</span>Method <span style="color: #339933;">=</span> WebRequestMethods<span style="color: #339933;">.</span>Http<span style="color: #339933;">.</span>Post<span style="color: #339933;">;</span>
    request<span style="color: #339933;">.</span>ContentType <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #339933;">;</span>
    request<span style="color: #339933;">.</span>ContentLength <span style="color: #339933;">=</span> message<span style="color: #339933;">.</span>Length<span style="color: #339933;">;</span>
    request<span style="color: #339933;">.</span>Headers<span style="color: #339933;">.</span>Add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;X-MessageID&quot;</span><span style="color: #339933;">,</span> Guid<span style="color: #339933;">.</span>NewGuid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ToString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    request<span style="color: #339933;">.</span>Headers<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;X-WindowsPhone-Target&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;toast&quot;</span><span style="color: #339933;">;</span>
    request<span style="color: #339933;">.</span>Headers<span style="color: #339933;">.</span>Add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;X-NotificationClass&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> requestStream <span style="color: #339933;">=</span> request<span style="color: #339933;">.</span>GetRequestStream<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    requestStream<span style="color: #339933;">.</span>Write<span style="color: #009900;">&#40;</span>message<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> message<span style="color: #339933;">.</span>Length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Das Binding in der App.config muss auf basicHttpBinding geändert werden:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>endpoint address <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> binding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;basicHttpBinding&quot;</span> contract<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;WindowsPhonePushNotificationService.IService1&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h2>WPF Push Application</h2>
<p>Den eben erstellen WCF Service werden wir hier in der WPF Applikation als Service Reference hinzufügen.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZV80LnBuZw=="><img class="alignnone size-full wp-image-5858" title="PushNotificationService_4" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService_4.png" alt="" width="500" /></a></p>
<p>Die WPF Applikation versendet unter Verwendung des WCF Services eine Nachricht an das Windows Phone. Hier werden wir lediglich zwei Textboxen für Titel und Nachricht und einen Button zum Absenden erstellen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>StackPanel<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>Label Content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Titel&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>TextBox Name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;txtBoxTitle&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;</span>Label Content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Nachricht&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>TextBox Height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;50&quot;</span> AcceptsReturn<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;True&quot;</span> Name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;txtBoxMessage&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;</span>Button Content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Push Message&quot;</span> Click<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Button_Click&quot;</span> Height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;25&quot;</span> Width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;150&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>StackPanel<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Der Button ruft nun die Methode SendToast am ServiceClient auf. Da der NotificationService manchmal etwas Zeit benötigt, packen wir den Aufruf in einen BackgroundWorker.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> void Button_Click<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> RoutedEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Title <span style="color: #339933;">=</span> txtBoxTitle<span style="color: #339933;">.</span>Text<span style="color: #339933;">;</span>
    Message <span style="color: #339933;">=</span> txtBoxMessage<span style="color: #339933;">.</span>Text<span style="color: #339933;">;</span>
&nbsp;
    BackgroundWorker worker <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BackgroundWorker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    worker<span style="color: #339933;">.</span>DoWork <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">new</span> DoWorkEventHandler<span style="color: #009900;">&#40;</span>worker_DoWork<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    worker<span style="color: #339933;">.</span>RunWorkerAsync<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
void worker_DoWork<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> DoWorkEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    using <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Service1Client<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        client<span style="color: #339933;">.</span>SendToast<span style="color: #009900;">&#40;</span>Title<span style="color: #339933;">,</span> Message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><br class="spacer_" /></p>
<h2><strong>Windows Phone 7 Push Notification Implementierung</strong></h2>
<p>Wie in der WPF Anwendung müssen wir auch hier den WCF Service referenzieren. Visual Studio erstellt nun eine Datei namens &#8220;ServiceReferences.ClientConfig&#8221;. Diese müssen wir nun öffnen und den Service konfigurieren.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>configuration<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>system<span style="color: #339933;">.</span>web<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>compilation debug<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span><span style="color: #990000;">system</span><span style="color: #339933;">.</span>web<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>system<span style="color: #339933;">.</span>serviceModel<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>bindings<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>basicHttpBinding<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>binding name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myService&quot;</span> closeTimeout<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;00:01:00&quot;</span>
          openTimeout<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;00:01:00&quot;</span> receiveTimeout<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;00:10:00&quot;</span> sendTimeout<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;00:01:00&quot;</span>
          maxReceivedMessageSize<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;65536&quot;</span> <span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>binding name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;BasicHttpBinding_IService1&quot;</span> maxBufferSize<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2147483647&quot;</span>
          maxReceivedMessageSize<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2147483647&quot;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>security mode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;None&quot;</span> <span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>binding<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>basicHttpBinding<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>bindings<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>client<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>endpoint address<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://localhost:8732/Design_Time_Addresses/WindowsPhonePushNotificationService/Service1/&quot;</span>
        binding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;basicHttpBinding&quot;</span> bindingConfiguration<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myService&quot;</span>
        contract<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ServiceReference1.IService1&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myService&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;/</span>client<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span><span style="color: #990000;">system</span><span style="color: #339933;">.</span>serviceModel<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>configuration<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Nun ziehen wir uns im Designer eine TextBox auf unser Phone und setzen diese etwas in Position.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZV81LnBuZw=="><img class="alignnone size-full wp-image-5860" title="PushNotificationService_5" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService_5.png" alt="" width="361" height="474" /></a></p>
<p>Nun wechseln wir in die  CodeAnsicht und implementieren folgenden Code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> HttpNotificationChannel _channel<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> string _channelName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MeinPushNotificationService&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> MainPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    InitializeComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    SetupNotificationChannel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> void SetupNotificationChannel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    _channel <span style="color: #339933;">=</span> HttpNotificationChannel<span style="color: #339933;">.</span>Find<span style="color: #009900;">&#40;</span>_channelName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_channel <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        _channel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpNotificationChannel<span style="color: #009900;">&#40;</span>_channelName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        _channel<span style="color: #339933;">.</span>ChannelUriUpdated <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>notificationChannel_ChannelUriUpdated<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _channel<span style="color: #339933;">.</span>Open<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
        RegisterForNotfication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> void notificationChannel_ChannelUriUpdated<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> NotificationChannelUriEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    _channel <span style="color: #339933;">=</span> HttpNotificationChannel<span style="color: #339933;">.</span>Find<span style="color: #009900;">&#40;</span>_channelName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>_channel<span style="color: #339933;">.</span>IsShellToastBound<span style="color: #009900;">&#41;</span>
        _channel<span style="color: #339933;">.</span>BindToShellToast<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    RegisterForNotfication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> void RegisterForNotfication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ServiceReference1<span style="color: #339933;">.</span>Service1Client<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client<span style="color: #339933;">.</span>SubscribeAsync<span style="color: #009900;">&#40;</span>_channel<span style="color: #339933;">.</span>ChannelUri<span style="color: #339933;">.</span>ToString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client<span style="color: #339933;">.</span>SubscribeCompleted <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">new</span> EventHandler<span style="color: #339933;">&lt;</span>System<span style="color: #339933;">.</span>ComponentModel<span style="color: #339933;">.</span>AsyncCompletedEventArgs<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>client_SubscribeCompleted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _channel<span style="color: #339933;">.</span>ShellToastNotificationReceived <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=&gt;</span> Deployment<span style="color: #339933;">.</span><span style="color: #990000;">Current</span><span style="color: #339933;">.</span>Dispatcher<span style="color: #339933;">.</span>BeginInvoke<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=&gt;</span> ToastReceived<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
void client_SubscribeCompleted<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> <span style="color: #990000;">System</span><span style="color: #339933;">.</span>ComponentModel<span style="color: #339933;">.</span>AsyncCompletedEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    textBlock1<span style="color: #339933;">.</span>Text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PushService active&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> void ToastReceived<span style="color: #009900;">&#40;</span>NotificationEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    textBlock1<span style="color: #339933;">.</span>Text <span style="color: #339933;">=</span> String<span style="color: #339933;">.</span>Format<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Title: {0} <span style="color: #000099; font-weight: bold;">\n</span>Message: {1}&quot;</span><span style="color: #339933;">,</span> e<span style="color: #339933;">.</span>Collection<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;wp:Text1&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> e<span style="color: #339933;">.</span>Collection<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;wp:Text2&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><br class="spacer_" /></p>
<h2>Projekt starten</h2>
<p>Als Startprojekte legen wir alle Projekte fest, die sich in der Solution befinden.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZV8yLnBuZw=="><img class="alignnone size-full wp-image-5854" title="PushNotificationService_2" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService_2.png" alt="" width="284" height="343" /></a></p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZV8zLnBuZw=="><img class="alignnone size-full wp-image-5855" title="PushNotificationService_3" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService_3.png" alt="" width="500" /></a></p>
<p><br class="spacer_" /></p>
<p>Jetzt debuggen wir das Ganze mit F5. Wenn in der WP7 Anwendung &#8220;PushService active&#8221; erscheint, wurde vom  MPN Service eine URL zugwiesen.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZV82LnBuZw=="><img class="alignnone size-full wp-image-5862" title="PushNotificationService_6" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService_6.png" alt="" width="242" height="275" /></a></p>
<p>Und wir können in der WPF Applikation Title und Nachricht eingeben und absenden. Wenn alles glatt geht, sollte im Windows Phone Emulator nun die Nachricht erscheinen:</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9QdXNoTm90aWZpY2F0aW9uU2VydmljZS5wbmc="><img class="alignnone size-full wp-image-5856" title="PushNotificationService" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/PushNotificationService.png" alt="" width="500" /></a></p>
<p><br class="spacer_" /></p>
<p>Wer möchte kann sich das Beispielprojekt <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy9kb3dubG9hZC9XaW5kb3dzUGhvbmVQdXNoTm90aWZpY2F0aW9uLnppcA==">hier</a> herunterladen.</p>
<p>Viel Spaß beim entwickeln: )</p>
<p><br class="spacer_" /></p>
<p><strong>Informationsmaterial</strong>:</p>
<ul>
<li><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21zZG4ubWljcm9zb2Z0LmNvbS9lbi11cy9saWJyYXJ5L2ZmNDAyNTU4KFZTLjkyKS5hc3B4">Überlick im MSDN</a></li>
<li>Using Push Notification im <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoYW5uZWw5Lm1zZG4uY29tL2xlYXJuL2NvdXJzZXMvV1A3VHJhaW5pbmdLaXQvV1A3U2lsdmVybGlnaHQvVXNpbmdQdXNoTm90aWZpY2F0aW9uc0xhYg==">WP7 TrainingsKit auf Channel9</a></li>
<li><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93aW5kb3dzLXBob25lLTctcmVzb3VyY2VzLWxpbmtzLXZpZGVvdHJhaW5pbmdz">Resources, Links &amp; Videotrainings</a></li>
</ul>
<hr /><p style="float:right; font-size:0.9em;">Dieser Beitrag stammt von <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGU=">Mario Priebe</a>.</p> <img src="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5850" width="1" height="1" style="display: none;" /><h2  class="related_post_title">Ähnliche Beiträge</h2><ul class="related_post"><li>27. Juli 2011 -- <a href="http://www.biggle.de/blog/the-neutralresourcelanguage-attribute-is-missing-on-the-entry-assembly" title="The [NeutralResourceLanguage] attribute is missing on the entry assembly">The [NeutralResourceLanguage] attribute is missing on the entry assembly</a></li><li>24. April 2011 -- <a href="http://www.biggle.de/blog/wasserzeichen-textbox-windows-phone-7" title="Wasserzeichen-TextBox Windows Phone 7">Wasserzeichen-TextBox Windows Phone 7</a></li><li>24. April 2011 -- <a href="http://www.biggle.de/blog/theme-resources-im-windows-phone-7" title="Theme Resources im Windows Phone 7">Theme Resources im Windows Phone 7</a></li><li>31. März 2011 -- <a href="http://www.biggle.de/blog/wie-ermittle-ich-eine-telefonnummer-aus-dem-telefonbuch-%e2%80%93-wp7-quicky" title="Wie ermittle ich eine Telefonnummer aus dem Telefonbuch? – WP7 Quicky">Wie ermittle ich eine Telefonnummer aus dem Telefonbuch? – WP7 Quicky</a></li><li>27. März 2011 -- <a href="http://www.biggle.de/blog/lesen-schreiben-und-loschen-im-isolatedstorage" title="Lesen, schreiben und löschen im IsolatedStorage">Lesen, schreiben und löschen im IsolatedStorage</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.biggle.de/blog/windows-phone-7-push-notification-implementierung-beispiel/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bing API Beispiel &#8211; C# Quicky</title>
		<link>http://www.biggle.de/blog/bing-api-beispiel-csharp-quicky</link>
		<comments>http://www.biggle.de/blog/bing-api-beispiel-csharp-quicky#comments</comments>
		<pubDate>Mon, 01 Nov 2010 15:54:09 +0000</pubDate>
		<dc:creator>Mario Priebe</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Quicky]]></category>

		<guid isPermaLink="false">http://www.biggle.de/blog/?p=5585</guid>
		<description><![CDATA[Am folgenden Beispiel möchte ich zeigen, wie man die Bing Api verwendet. Vorerst muss man sich im Bing Developer Center einen API Key registrieren. Das geht recht einfach und ist schnell erledigt. Nun fügt man seinem Projekt eine ServiceReferenz über folgender URL hinzu: http://api.bing.net/search.wsdl?AppID=YourAppId&#38;amp;Version=2.2 YourAppId muss mit dem Key ersetzt werden. Der nachfolgende Code zeigt [...]]]></description>
			<content:encoded><![CDATA[<p>Am folgenden Beispiel möchte ich zeigen, wie man die Bing Api verwendet.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8xMS9CaW5nU3VjaGUucG5n"><img class="alignnone size-full wp-image-5587" title="BingSuche" src="http://www.biggle.de/blog/wp-content/uploads/2010/11/BingSuche.png" alt="" width="500" /></a></p>
<p>Vorerst muss man sich im Bing Developer Center einen <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaW5nLmNvbS9kZXZlbG9wZXJzL2NyZWF0ZWFwcC5hc3B4">API Key registrieren</a>. Das geht recht einfach und ist schnell erledigt.</p>
<p>Nun fügt man seinem Projekt eine ServiceReferenz über folgender URL hinzu:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//api.bing.net/search.wsdl?AppID=YourAppId&amp;amp;Version=2.2</span></pre></div></div>

<p>YourAppId muss mit dem Key ersetzt werden.</p>
<p>Der nachfolgende Code zeigt eine Beispiels-Implementierung in einer Konsolenapplikation.</p>
<p>Klasse Bing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">using <span style="color: #990000;">System</span><span style="color: #339933;">;</span>
using BIngApiTest23432<span style="color: #339933;">.</span>BingService<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Threading<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> BIngApiTest23432
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bing
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> static string Api <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> string Url <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> string DisplayUrl <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> string Title <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> string Desc <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> bool IsLoading <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> SourceType SourceType <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> Bing<span style="color: #009900;">&#40;</span>string api<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> Api <span style="color: #339933;">=</span> api<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> SearchResponse Lookup<span style="color: #009900;">&#40;</span>string query<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            IsLoading <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            Thread tr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Thread<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ThreadStart<span style="color: #009900;">&#40;</span>Loading<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tr<span style="color: #339933;">.</span>Start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SearchResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            using <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> service <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BingPortTypeClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                try
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SearchRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    request<span style="color: #339933;">.</span>AppId <span style="color: #339933;">=</span> Api<span style="color: #339933;">;</span>
                    request<span style="color: #339933;">.</span>Query <span style="color: #339933;">=</span> query<span style="color: #339933;">;</span>
                    request<span style="color: #339933;">.</span>Sources <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SourceType<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> SourceType <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
                    response <span style="color: #339933;">=</span> service<span style="color: #339933;">.</span>Search<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    IsLoading <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                catch <span style="color: #009900;">&#40;</span><span style="color: #990000;">System</span><span style="color: #339933;">.</span>Net<span style="color: #339933;">.</span>WebException ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">return</span> response<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> void Loading<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>IsLoading<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                Console<span style="color: #339933;">.</span>Write<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                Thread<span style="color: #339933;">.</span><span style="color: #990000;">Sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Klasse Program</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">using <span style="color: #990000;">System</span><span style="color: #339933;">;</span>
using BIngApiTest23432<span style="color: #339933;">.</span>BingService<span style="color: #339933;">;</span>
using Microsoft<span style="color: #339933;">.</span>CSharp<span style="color: #339933;">.</span>RuntimeBinder<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Diagnostics<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> BIngApiTest23432
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">class</span> Program
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">const</span> string API <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR API KEY&quot;</span><span style="color: #339933;">;</span>
        static List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> _urls<span style="color: #339933;">;</span>
&nbsp;
        static void Main<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            CallBingMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> static void CallBingMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            Bing bing <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bing<span style="color: #009900;">&#40;</span>API<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">var</span> menu <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;choose the search :<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>(W)eb, (I)mage, (S)pell, (R)elated Search, (P)honebook, (C)lear, E(x)it&quot;</span><span style="color: #339933;">;</span>
&nbsp;
            Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span>menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            ConsoleKeyInfo <span style="color: #990000;">key</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">do</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #990000;">key</span> <span style="color: #339933;">=</span> Console<span style="color: #339933;">.</span>ReadKey<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                SearchResponse response <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SearchResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">case</span> ConsoleKey<span style="color: #339933;">.</span>C<span style="color: #339933;">:</span>
                        Console<span style="color: #339933;">.</span>Clear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">case</span> ConsoleKey<span style="color: #339933;">.</span>W<span style="color: #339933;">:</span>
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>(W)ebsuche <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Bitte Begriff eingeben: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        bing<span style="color: #339933;">.</span>SourceType <span style="color: #339933;">=</span> SourceType<span style="color: #339933;">.</span>Web<span style="color: #339933;">;</span>
                        response <span style="color: #339933;">=</span> bing<span style="color: #339933;">.</span>Lookup<span style="color: #009900;">&#40;</span>Console<span style="color: #339933;">.</span>ReadLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">case</span> ConsoleKey<span style="color: #339933;">.</span>I<span style="color: #339933;">:</span>
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>(I)magesuche <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Bitte Begriff eingeben: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        bing<span style="color: #339933;">.</span>SourceType <span style="color: #339933;">=</span> SourceType<span style="color: #339933;">.</span>Image<span style="color: #339933;">;</span>
                        response <span style="color: #339933;">=</span> bing<span style="color: #339933;">.</span>Lookup<span style="color: #009900;">&#40;</span>Console<span style="color: #339933;">.</span>ReadLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">case</span> ConsoleKey<span style="color: #339933;">.</span>S<span style="color: #339933;">:</span>
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>(S)pellsuche <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Bitte Begriff eingeben: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        bing<span style="color: #339933;">.</span>SourceType <span style="color: #339933;">=</span> SourceType<span style="color: #339933;">.</span>Spell<span style="color: #339933;">;</span>
                        response <span style="color: #339933;">=</span> bing<span style="color: #339933;">.</span>Lookup<span style="color: #009900;">&#40;</span>Console<span style="color: #339933;">.</span>ReadLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">case</span> ConsoleKey<span style="color: #339933;">.</span>R<span style="color: #339933;">:</span>
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>(R)elated Search <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Bitte Begriff eingeben: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        bing<span style="color: #339933;">.</span>SourceType <span style="color: #339933;">=</span> SourceType<span style="color: #339933;">.</span>RelatedSearch<span style="color: #339933;">;</span>
                        response <span style="color: #339933;">=</span> bing<span style="color: #339933;">.</span>Lookup<span style="color: #009900;">&#40;</span>Console<span style="color: #339933;">.</span>ReadLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">case</span> ConsoleKey<span style="color: #339933;">.</span>P<span style="color: #339933;">:</span>
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>(P)honebook Search <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Bitte Begriff eingeben: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        bing<span style="color: #339933;">.</span>SourceType <span style="color: #339933;">=</span> SourceType<span style="color: #339933;">.</span>Phonebook<span style="color: #339933;">;</span>
                        response <span style="color: #339933;">=</span> bing<span style="color: #339933;">.</span>Lookup<span style="color: #009900;">&#40;</span>Console<span style="color: #339933;">.</span>ReadLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span> <span style="color: #339933;">!=</span> ConsoleKey<span style="color: #339933;">.</span>C <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span> <span style="color: #339933;">!=</span> ConsoleKey<span style="color: #339933;">.</span>X<span style="color: #009900;">&#41;</span>
                    DisplayResult<span style="color: #009900;">&#40;</span>response<span style="color: #339933;">,</span> bing<span style="color: #339933;">.</span>SourceType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>_urls <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> _urls<span style="color: #339933;">.</span><span style="color: #990000;">Count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;choose the number to call the url in your browser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #990000;">key</span> <span style="color: #339933;">=</span> Console<span style="color: #339933;">.</span>ReadKey<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span> <span style="color: #339933;">&gt;=</span> ConsoleKey<span style="color: #339933;">.</span>D0 <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span> <span style="color: #339933;">&lt;=</span> ConsoleKey<span style="color: #339933;">.</span>D9<span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">var</span> k <span style="color: #339933;">=</span> Convert<span style="color: #339933;">.</span>ToInt32<span style="color: #009900;">&#40;</span><span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span><span style="color: #339933;">.</span>ToString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>TrimStart<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        k <span style="color: #339933;">=</span> k <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> ? <span style="color: #cc66cc;">10</span> <span style="color: #339933;">:</span> k<span style="color: #339933;">;</span>
&nbsp;
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; - you choosed: {0}&quot;</span><span style="color: #339933;">,</span> _urls<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        Process<span style="color: #339933;">.</span>Start<span style="color: #009900;">&#40;</span>_urls<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #009900;">&#125;</span>
&nbsp;
                Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">+</span> menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">key</span><span style="color: #339933;">.</span><span style="color: #990000;">Key</span> <span style="color: #339933;">!=</span> ConsoleKey<span style="color: #339933;">.</span>X<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//|| key.Modifiers != ConsoleModifiers.Alt);</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> static void DisplayResult<span style="color: #009900;">&#40;</span>SearchResponse searchResponse<span style="color: #339933;">,</span> SourceType sourceType<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            dynamic results <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            _urls <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            _urls<span style="color: #339933;">.</span>Add<span style="color: #009900;">&#40;</span>String<span style="color: #339933;">.</span><span style="color: #990000;">Empty</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>sourceType<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>Spell<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>Spell <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>Spell<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>Web<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>Web <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>Web<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>Image<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>Image <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>Image<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>RelatedSearch<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>RelatedSearch <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>RelatedSearch<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>Phonebook<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>Phonebook <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>Phonebook<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>Video<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>Video<span style="color: #339933;">.</span>Results <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>Video<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>InstantAnswer<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>InstantAnswer<span style="color: #339933;">.</span>Results <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>InstantAnswer<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>News<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>News<span style="color: #339933;">.</span>Results <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>News<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>MobileWeb<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>MobileWeb<span style="color: #339933;">.</span>Results <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>MobileWeb<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">case</span> SourceType<span style="color: #339933;">.</span>Translation<span style="color: #339933;">:</span>
                    results <span style="color: #339933;">=</span> searchResponse<span style="color: #339933;">.</span>Translation<span style="color: #339933;">.</span>Results <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> ? searchResponse<span style="color: #339933;">.</span>Translation<span style="color: #339933;">.</span>Results <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
                    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>results <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Result:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> result in results<span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    try
                    <span style="color: #009900;">&#123;</span>
                        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span>result<span style="color: #339933;">.</span>Url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        _urls<span style="color: #339933;">.</span>Add<span style="color: #009900;">&#40;</span>result<span style="color: #339933;">.</span>Url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    catch <span style="color: #009900;">&#40;</span>RuntimeBinderException<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span>result<span style="color: #339933;">.</span>Value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span>
                Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Leider nichts gefunden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Resources:</p>
<ul>
<li><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaW5nLmNvbS9kZXZlbG9wZXJzL3MvQVBJJTIwQmFzaWNzLnBkZg==">Basics</a> [pdf]</li>
<li><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21zZG4ubWljcm9zb2Z0LmNvbS9lbi11cy9saWJyYXJ5L2RkMjUxMDU2LmFzcHg=">Dokumentation</a> Version 2</li>
<li><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2hlbHAubGl2ZS5jb20vaGVscC5hc3B4P21rdD1lbi11cyZhbXA7cHJvamVjdD1zZWFyY2hkZXZjdHImYW1wO3F1ZXJ5dHlwZT1rZXl3b3JkJmFtcDtxdWVyeT1mYXE=">Bing FAQ</a></li>
</ul>
<p>Viel Spaß beim entwickeln : )</p>
<p><br class="spacer_" /></p>
<hr /><p style="float:right; font-size:0.9em;">Dieser Beitrag stammt von <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGU=">Mario Priebe</a>.</p> <img src="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5585" width="1" height="1" style="display: none;" /><h2  class="related_post_title">Ähnliche Beiträge</h2><ul class="related_post"><li>27. November 2009 -- <a href="http://www.biggle.de/blog/aufzaehlungen-speichern" title="Aufzählungen speichern &#8211; C# Quicky">Aufzählungen speichern &#8211; C# Quicky</a></li><li>4. November 2011 -- <a href="http://www.biggle.de/blog/wie-verhindere-ich-beim-klick-eines-hyperlinkbuttons-in-silverlight-die-umrandung-quicky" title="Wie verhindere ich beim Klick eines HyperlinkButtons in Silverlight die Umrandung &#8211; Quicky">Wie verhindere ich beim Klick eines HyperlinkButtons in Silverlight die Umrandung &#8211; Quicky</a></li><li>31. März 2011 -- <a href="http://www.biggle.de/blog/wie-ermittle-ich-eine-telefonnummer-aus-dem-telefonbuch-%e2%80%93-wp7-quicky" title="Wie ermittle ich eine Telefonnummer aus dem Telefonbuch? – WP7 Quicky">Wie ermittle ich eine Telefonnummer aus dem Telefonbuch? – WP7 Quicky</a></li><li>14. Februar 2011 -- <a href="http://www.biggle.de/blog/texttrimming-xaml-quicky" title="TextTrimming &#8211;  XAML Quicky">TextTrimming &#8211;  XAML Quicky</a></li><li>14. Februar 2011 -- <a href="http://www.biggle.de/blog/doppelklick-im-datagrid-abfangen-wpf-quicky" title="Doppelklick im DataGrid abfangen &#8211; WPF Quicky">Doppelklick im DataGrid abfangen &#8211; WPF Quicky</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.biggle.de/blog/bing-api-beispiel-csharp-quicky/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Einfaches Beispiel eines asynchronen Methodenaufrufs</title>
		<link>http://www.biggle.de/blog/einfaches-beispiel-eines-asynchronen-methodenaufrufs</link>
		<comments>http://www.biggle.de/blog/einfaches-beispiel-eines-asynchronen-methodenaufrufs#comments</comments>
		<pubDate>Thu, 12 Aug 2010 18:56:58 +0000</pubDate>
		<dc:creator>Mario Priebe</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://www.biggle.de/blog/?p=5117</guid>
		<description><![CDATA[Threading ist ein sehr komplexes Thema und es gibt auch genügend Alternativen Prozesse zeitgleich zu starten. Siehe BackgroundWorker, Task, Linqextension Parallel und Konsorten sowie asynchronen Methoden an WebServices und mehr&#8230; Im Folgenden wird an einem recht einfachen Beispiel versucht zu veranschaulichen, wie man Arbeitsaufgaben mit einfachen .NET Mitteln parallel erledigen kann. Also wie man quasi [...]]]></description>
			<content:encoded><![CDATA[<p>Threading ist ein sehr komplexes Thema und es gibt auch genügend Alternativen Prozesse zeitgleich zu starten. Siehe BackgroundWorker, Task, Linqextension Parallel und Konsorten sowie asynchronen Methoden an WebServices und mehr&#8230;</p>
<p>Im Folgenden wird an einem recht einfachen Beispiel versucht zu veranschaulichen, wie man Arbeitsaufgaben mit einfachen .NET Mitteln parallel erledigen kann. Also wie man quasi eine Methode in einem anderen Thread aufrufen kann, die die Arbeit im Hintergrund erledigen soll ohne den anderen, aufrufenden Thread zu blockieren.</p>
<p>Ich erstelle mir dazu eine Methode welche aus der Programmlogik aufgerufen werden soll. Diese verwendet eine weitere Methode, die die eigentliche Arbeit übernimmt. Damit ich der asynchronen Methode auch Parameter mitgeben kann, verwende ich zusätzlich der normalen Thread-Klasse die Klasse ParameterizedThreadStart, weil es durch diese ermöglicht wird, Parameter an die Jobmethode weiterzuleiten.</p>
<p>Die notwendigen Parameter halte ich in der Klasse Namens &#8220;AsyncData&#8221;, die zu den Daten auch eine boolesche Variable beinhaltet. Mit dieser Variable ist es mir möglich zu prüfen wann der asynchrone Prozess denn erledigt ist. Damit ich keine Problem bekomme, wenn beide Threads auf diese Variable zugreifen wollen verwende ich das Schlüsselwort <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21zZG4ubWljcm9zb2Z0LmNvbS9kZS1kZS9saWJyYXJ5L3gxM3R0d3c3KFZTLjgwKS5hc3B4">volatile</a>.</p>
<p>Klasse Asyncdata:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">class</span> AsyncData
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> Personen <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> int PersonenRequired <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> volatile bool IsCompleted<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Dieses Methode erstellt die den Arbeitsthread und lässt über eine weitere Methode den Job erledigen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> static void GetPersonen<span style="color: #009900;">&#40;</span>AsyncData data<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    ParameterizedThreadStart pts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParameterizedThreadStart<span style="color: #009900;">&#40;</span>GetPersonenJob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Thread thread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Thread<span style="color: #009900;">&#40;</span>pts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    thread<span style="color: #339933;">.</span>Start<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Die folgende Methode erledigt den Job und setzt die boolesche Variable, wenn die Arbeit erledigt ist auf  true</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> static void GetPersonenJob<span style="color: #009900;">&#40;</span>object result<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    AsyncData asyncData <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>AsyncData<span style="color: #009900;">&#41;</span>result<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Beispiel Job, der viel zeit in Anspruch nimmt (ggfl. Anzahl der Personen erhoehen)</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>int i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> asyncData<span style="color: #339933;">.</span>PersonenRequired<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> Age <span style="color: #339933;">=</span> i<span style="color: #339933;">,</span> Firstname <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bla&quot;</span> <span style="color: #339933;">+</span> i<span style="color: #339933;">,</span> Name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blubb&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        asyncData<span style="color: #339933;">.</span>Personen<span style="color: #339933;">.</span>Add<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//Job erledigt</span>
    asyncData<span style="color: #339933;">.</span>IsCompleted <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Der Aufruf im Programm, stellt sich wie folgt dar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">static void Main<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    AsyncData asyncData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AsyncData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    asyncData<span style="color: #339933;">.</span>PersonenRequired <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span>
    GetPersonen<span style="color: #009900;">&#40;</span>asyncData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//an der Stelle verwende ich die boolesche Variable und zu pruefen ob der Job erledigt ist.</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>asyncData<span style="color: #339933;">.</span>IsCompleted<span style="color: #009900;">&#41;</span>
        Thread<span style="color: #339933;">.</span><span style="color: #990000;">Sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> person in asyncData<span style="color: #339933;">.</span>Personen<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Console<span style="color: #339933;">.</span>WriteLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{0},{1} Alter: {2}&quot;</span><span style="color: #339933;">,</span> person<span style="color: #339933;">.</span>Name<span style="color: #339933;">,</span> person<span style="color: #339933;">.</span>Firstname<span style="color: #339933;">,</span> person<span style="color: #339933;">.</span>Age<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    Console<span style="color: #339933;">.</span>ReadLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>//Beispielklasse</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">internal <span style="color: #000000; font-weight: bold;">class</span> Person
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">public</span> string Name <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> string Firstname <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> int Age <span style="color: #009900;">&#123;</span> get<span style="color: #339933;">;</span> set<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Viel Spass beim entwickeln : )</p>
<hr /><p style="float:right; font-size:0.9em;">Dieser Beitrag stammt von <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGU=">Mario Priebe</a>.</p> <img src="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5117" width="1" height="1" style="display: none;" /><h2  class="related_post_title">Ähnliche Beiträge</h2><ul class="related_post"><li>3. Dezember 2010 -- <a href="http://www.biggle.de/blog/biggle-ist-kein-palindrom" title="Biggle ist kein Palindrom">Biggle ist kein Palindrom</a></li><li>16. November 2010 -- <a href="http://www.biggle.de/blog/windows-phone-7-push-notification-implementierung-beispiel" title="Windows Phone 7 – Push Notification">Windows Phone 7 – Push Notification</a></li><li>1. November 2010 -- <a href="http://www.biggle.de/blog/bing-api-beispiel-csharp-quicky" title="Bing API Beispiel &#8211; C# Quicky">Bing API Beispiel &#8211; C# Quicky</a></li><li>13. April 2010 -- <a href="http://www.biggle.de/blog/das-wpf-control-popup-howto" title="Das WPF Control &#8220;Popup&#8221; &#8211; HowTo">Das WPF Control &#8220;Popup&#8221; &#8211; HowTo</a></li><li>7. April 2010 -- <a href="http://www.biggle.de/blog/wcfservice-testen" title="WCFService testen">WCFService testen</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.biggle.de/blog/einfaches-beispiel-eines-asynchronen-methodenaufrufs/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Das WPF Control &#8220;Popup&#8221; &#8211; HowTo</title>
		<link>http://www.biggle.de/blog/das-wpf-control-popup-howto</link>
		<comments>http://www.biggle.de/blog/das-wpf-control-popup-howto#comments</comments>
		<pubDate>Tue, 13 Apr 2010 15:35:00 +0000</pubDate>
		<dc:creator>Mario Priebe</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[WPFControls]]></category>

		<guid isPermaLink="false">http://www.biggle.de/blog/?p=4446</guid>
		<description><![CDATA[Das WPF Control Popup kann ein eigenständiges Fenster in einer WPF Applikation darstellen und wie ein Tooltip beim Überfahren von Elementen angezeigt werden. Ein Popup kann als Kindelement ein weiteres UIElement aufnehmen, was so einem weiteren Design des Popups, nichts im Wege stehen sollte. Die Position kann relativ zum Element oder zur Applikation geöffnet werden. [...]]]></description>
			<content:encoded><![CDATA[<p>Das WPF Control Popup kann ein eigenständiges Fenster in einer WPF Applikation darstellen und wie ein Tooltip beim Überfahren von Elementen angezeigt werden. Ein Popup kann als Kindelement ein weiteres UIElement aufnehmen, was so einem weiteren Design des Popups, nichts im Wege stehen sollte.</p>
<p>Die Position kann relativ zum Element oder zur Applikation geöffnet werden. Das Popup verhält sich auch wie ein eigenständiges Fenster, das heisst wird die Mainapplikation verschoben, bleibt das Popup an der letzten Position stehen.</p>
<p><a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGUvYmxvZy93cC1jb250ZW50L3VwbG9hZHMvMjAxMC8wNC9wb3B1cC5qcGc="><img src="http://www.biggle.de/blog/wp-content/uploads/2010/04/popup.jpg" alt="" title="popup" width="426" height="345" class="alignnone size-full wp-image-4457" /></a></p>
<p>Ein Popup wird nicht wie ein Tooltip automatisch geöffnet, sondern muss explizit durch dessen Eigenschaft IsOpen geöffnet und geschlossen werden.</p>
<p>Die Anzeigeposition wird durch die Placement-Eigenschaften festgelegt. Zu welchem Element sich die Placements beziehen, legt man an der Property PlacementTarget fest. Die X und die Y Koordinate HorizontalOffset und VertracalOffset setzt man bei einem gewählten &#8220;Absolute&#8221; oder &#8220;Relative&#8221; Placement aus der Enumeration PlacementMode.</p>
<p>Weitere Eigenschaften an dem Control sind PopupAnimation und AllowsTransparency. Will man ein Popup animieren (Fade, None, Scroll, Slide) muss AllowsTransparency auf true gesetzt werden.</p>
<p>Das folgende Beispiel soll das gesagte etwas verdeutlichen. Hier beziehe ich mein Placement relativ zur Applikation(window), den Bezug stellt man über ElementName her.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>Window x<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">Class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PopupHowTo.Window1&quot;</span>
    xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span>
    xmlns<span style="color: #339933;">:</span>x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span>
    Title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Window1&quot;</span> Height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;300&quot;</span> Width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;300&quot;</span> Name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myWindow&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>StackPanel HorizontalAlignment<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Left&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>TextBox Width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> Name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;txtBox&quot;</span> MouseEnter<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;txtBox_MouseEnter&quot;</span> MouseLeave<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;txtBox_MouseLeave&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>Popup Name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;popUp&quot;</span> PlacementTarget<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{Binding ElementName=myWindow}&quot;</span> Placement<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Relative&quot;</span> VerticalOffset<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;30&quot;</span> HorizontalOffset<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span>
   PopupAnimation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Slide&quot;</span> AllowsTransparency<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>Popup<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>StackPanel<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>Window<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>CodeBehind</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> partial <span style="color: #000000; font-weight: bold;">class</span> Window1 <span style="color: #339933;">:</span> Window
<span style="color: #009900;">&#123;</span>
MyUserControl uc<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> Window1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    InitializeComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Loaded <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">new</span> RoutedEventHandler<span style="color: #009900;">&#40;</span>Window1_Loaded<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
void Window1_Loaded<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> RoutedEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    uc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyUserControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    popUp<span style="color: #339933;">.</span>Child <span style="color: #339933;">=</span> uc<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> void txtBox_MouseEnter<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> MouseEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    uc<span style="color: #339933;">.</span>HelpText <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich bin ein Hilfetext für eine TextBox&quot;</span><span style="color: #339933;">;</span>
    popUp<span style="color: #339933;">.</span>IsOpen <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> void txtBox_MouseLeave<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> MouseEventArgs e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    popUp<span style="color: #339933;">.</span>IsOpen <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>HelpText ist ein ViewModelProperty in MyUserControl.</p>
<p>Viel Spass beim entwickeln : )</p>
<p><br class="spacer_" /></p>
<hr /><p style="float:right; font-size:0.9em;">Dieser Beitrag stammt von <a href="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5iaWdnbGUuZGU=">Mario Priebe</a>.</p> <img src="http://www.biggle.de/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=4446" width="1" height="1" style="display: none;" /><h2  class="related_post_title">Ähnliche Beiträge</h2><ul class="related_post"><li>3. Dezember 2010 -- <a href="http://www.biggle.de/blog/biggle-ist-kein-palindrom" title="Biggle ist kein Palindrom">Biggle ist kein Palindrom</a></li><li>16. November 2010 -- <a href="http://www.biggle.de/blog/windows-phone-7-push-notification-implementierung-beispiel" title="Windows Phone 7 – Push Notification">Windows Phone 7 – Push Notification</a></li><li>1. November 2010 -- <a href="http://www.biggle.de/blog/bing-api-beispiel-csharp-quicky" title="Bing API Beispiel &#8211; C# Quicky">Bing API Beispiel &#8211; C# Quicky</a></li><li>23. Oktober 2010 -- <a href="http://www.biggle.de/blog/rating-wpf-control" title="Rating &#8211; WPF Control">Rating &#8211; WPF Control</a></li><li>12. August 2010 -- <a href="http://www.biggle.de/blog/einfaches-beispiel-eines-asynchronen-methodenaufrufs" title="Einfaches Beispiel eines asynchronen Methodenaufrufs">Einfaches Beispiel eines asynchronen Methodenaufrufs</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.biggle.de/blog/das-wpf-control-popup-howto/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

