AutoCompleteBox的使用和本地xml读取

     这几天自己研究了一下AutoCompleteBox和xml,想做个字典玩,╮(╯▽╰)╭真是看着简单做着难,wp7和winfrom差大了,纠结了老久。本人学生,自娱自乐,不足地方很多,贵在掺和嘛。

xaml
 1 <phone:PhoneApplicationPage 
2 x:Class="testai1.MainPage"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
10 xmlns:My="clr-namespace:testai1"
11 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
12 FontFamily="{StaticResource PhoneFontFamilyNormal}"
13 FontSize="{StaticResource PhoneFontSizeNormal}"
14 Foreground="{StaticResource PhoneForegroundBrush}"
15 SupportedOrientations="Portrait" Orientation="Portrait"
16 shell:SystemTray.IsVisible="True" xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
17
18 <!--LayoutRoot is the root grid where all page content is placed-->
19 <Grid x:Name="LayoutRoot" Background="Transparent">
20 <Grid.RowDefinitions>
21 <RowDefinition Height="Auto"/>
22 <RowDefinition Height="*"/>
23 </Grid.RowDefinitions>
24
25 <!--TitlePanel contains the name of the application and page title-->
26 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
27 <TextBlock x:Name="ApplicationTitle" Text="wxdtk1989" Style="{StaticResource PhoneTextNormalStyle}"/>
28 <TextBlock x:Name="PageTitle" Text="本地词典TEST" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
29 </StackPanel>
30
31 <!--ContentPanel - place additional content here-->
32 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
33 <toolkit:AutoCompleteBox HorizontalAlignment="Left" Margin="30,86,0,0" Name="DictAutoCompleteBox" VerticalAlignment="Top" Height="89" Width="397" TextChanged="DictAutoCompleteBox_TextChanged" IsTextCompletionEnabled="True" />
34 <ListBox HorizontalAlignment="Left" Margin="9,202,0,95" Name="listBox1" Width="443">
35 <ListBox.ItemTemplate>
36 <DataTemplate>
37 <StackPanel Orientation="Horizontal">
38 <StackPanel>
39 <TextBlock Text="{Binding name}" FontSize=" 24"/>
40 <TextBlock Text="{Binding content}" FontSize=" 24"/>
41 </StackPanel>
42 </StackPanel>
43 </DataTemplate>
44 </ListBox.ItemTemplate>
45 </ListBox>
46 <Button Content="读本地xml" Height="72" HorizontalAlignment="Left" Margin="12,518,0,0" Name="button1" VerticalAlignment="Top" Width="167" Click="button1_Click" />
47 <HyperlinkButton Content="在线词典" Height="50" HorizontalAlignment="Left" Margin="248,527,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="136" NavigateUri="/zaixiandict.xaml"/>
48 </Grid>
49 </Grid>
50 </phone:PhoneApplicationPage>
  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 using System.ComponentModel;
14 using System.IO;
15 using System.Text;
16 using System.IO.IsolatedStorage;
17 using System.Xml;
18 using System.Xml.Linq;
19 namespace testai1
20 {
21 public partial class MainPage : PhoneApplicationPage
22 {
23 // Constructor
24 public MainPage()
25 {
26 InitializeComponent();
27 dictionary dict1 = new dictionary();//自己写的类,就是get;set;之类的东西
28 List<String> names1 = new List<String>();
29 String namesString1 = dict1.a;
30 foreach (var name1 in namesString1.Split(']'))
31 names1.Add(name1);
32 this.DictAutoCompleteBox.ItemsSource = names1;
33 this.DictAutoCompleteBox.ItemFilter += SearchCountry;
34 }
35
36 //全局模糊搜索
37
38 bool SearchCountry(string search, object value)
39 {
40 if (value != null)
41 {
42 //如果包含了搜索的字符串则返回true
43 if (value.ToString().ToLower().IndexOf(search) >= 0)
44 return true;
45 }
46 // 如果不匹配 返回false
47 return false;
48 }
49
50 private void button1_Click(object sender, RoutedEventArgs e)
51 {
52 //String namesString;
53 XDocument xdoc = XDocument.Load("XMLFile1.xml");
54 var dict= from query in xdoc.Descendants ("DicEn")
55 select new dictionary
56 {
57 // id =query.Element ("ID").Value .ToString (),
58 name = query.Element("Name").Value.ToString(),
59 content = query.Element("Content").Value.ToString()
60 };
61 this.listBox1.ItemsSource = dict;
62 }
63
64 private void DictAutoCompleteBox_TextChanged(object sender, RoutedEventArgs e)
65 {
66 dictionary dict = new dictionary();
67 List<String> names = new List<String>();
68 String namesString=dict.a ;
69 if (DictAutoCompleteBox.Text.Length == 1)
70 {
71 char[] word = DictAutoCompleteBox.Text.ToCharArray();
72
73 switch (word[0]) //为了加载快些我把提示单词分24份,我想直接读txt文件,但提示不行,,,
74 {
75 case 'a': //各位谁给个访问txt的代码,IsolatedStorage的或者FileStream StreamRead
76 namesString = dict.a; break;
77 case 'b':
78 namesString = dict.b; break;
79 case 'c':
80 namesString = dict.c; break;
81 case 'd':
82 namesString = dict.d; break;
83 case 'e':
84 namesString = dict.e; break;
85 case 'f':
86 namesString = dict.f; break;
87 case 'g':
88 namesString = dict.g; break;
89 case 'h':
90 namesString = dict.h; break;
91 case 'i':
92 namesString = dict.i; break;
93 case 'j':
94 namesString = dict.j; break;
95 case 'k':
96 namesString = dict.k; break;
97 case 'l':
98 namesString = dict.l; break;
99 case 'm':
100 namesString = dict.m; break;
101 case 'n':
102 namesString = dict.n; break;
103 case 'o':
104 namesString = dict.o; break;
105 case 'p':
106 namesString = dict.p; break;
107 case 'q':
108 namesString = dict.q; break;
109 case 'r':
110 namesString = dict.r; break;
111 case 's':
112 namesString = dict.s; break;
113 case 't':
114 namesString = dict.t; break;
115 case 'u':
116 namesString = dict.u; break;
117 case 'v':
118 namesString = dict.v; break;
119 case 'w':
120 namesString = dict.w; break;
121 case 'x':
122 case 'y':
123 case 'z':
124 namesString = dict.xyz; break;
125 default:
126 DictAutoCompleteBox.Text = "输入有误!";
127 break;
128
129 }
130 foreach (var name in namesString.Split(']'))
131 names.Add(name);
132 this.DictAutoCompleteBox.ItemsSource = names;
133 this.DictAutoCompleteBox.ItemFilter += SearchCountry;
134 }
135 }
136 }
137 }

    方法可能很笨,见谅。在线字典正在纠结怎么加单词发音。以后再发吧

源码: http://u.115.com/file/dn6ciejg  移动网卡太不给力,传博客园失败,就传115了,  各位注意zaixiandict.xaml.cs中,listbox1我加了按钮,纠结的是怎么把<pron>. ....</pron>中的地址给按钮事件里的MediaElement  ,如果行不通,能有什么办法,,,,谢了

posted @ 2011-07-22 08:58  wxdtk1989  阅读(1698)  评论(6编辑  收藏  举报