1. 采用Isolated Storage存储DLL文件,在此存储位置的文件是Silverlight可以访问的,并且针对当前用户和所对应的xap文件(网址)是唯一的。采用这种方式可以只在用户第一次访问时下载一次,提升用户使用体验。(示例代码采用复制本地文件到Isolated Storage的方式,也可以用WebClient从服务器上获取)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 private static void LoadLocalFiles(TextBox slt)
2 {
3 OpenFileDialog ofd = new OpenFileDialog();
4 ofd.Filter = "DLL Files (*.dll)|*.dll";
5 ofd.Multiselect = true;
6
7 if (ofd.ShowDialog() == true)
8 {
9 using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
10 {
11 foreach (FileInfo file in ofd.Files)
12 {
13 if (store.FileExists(file.Name) == true)
14 {
15 slt.Text = "File " + file.Name + "existed";
16 return;
17 }
18 else
19 {
20 using (Stream fileStream = file.OpenRead())
21 {
22 if (fileStream.Length > store.AvailableFreeSpace)
23 {
24 if (store.IncreaseQuotaTo(store.Quota + fileStream.Length) == true)
25 {
26 slt.Text = "Quota increase succeeded";
27 }
28 else
29 {
30 slt.Text = "Quota increase failed";
31 return;
32 }
33 }
34
35 using (IsolatedStorageFileStream storeStream = store.CreateFile(file.Name))
36 {
37 byte[] buffer = new byte[1024];
38 int count = 0;
39
40 do
41 {
42 count = fileStream.Read(buffer, 0, buffer.Length);
43
44 if (count > 0)
45 {
46 storeStream.Write(buffer, 0, count);
47 }
48 } while (count > 0);
49 }
50 }
51 }
52 }
53 }
54 }
55 }
56
2. 采用Assembly加载DLL文件。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 void btnDemo_Click(object sender, RoutedEventArgs e)
2 {
3 try
4 {
5 using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
6 {
7 string dllToLoad = "SCLTextBox.dll";
8 if (store.FileExists(dllToLoad) == true)
9 {
10 using (IsolatedStorageFileStream stream = store.OpenFile(dllToLoad, FileMode.Open))
11 {
12 AssemblyPart assemblyPart = new AssemblyPart();
13 Assembly asm = assemblyPart.Load(stream);
14 Control ctrl = asm.CreateInstance("SCLTextBox.SLTextBox") as Control;
15 ctrl.Height = 100;
16 ctrl.Width = 100;
17 ctrl.BorderThickness = new Thickness(2, 4, 6, 9);
18 if (ctrl != null)
19 {
20 Canvas.SetLeft(ctrl, 300);
21 Canvas.SetTop(ctrl, 500);
22
23 LayoutRoot.Children.Add(ctrl);
24 }
25 }
26 }
27 }
28 }
29 catch (Exception ex)
30 {
31 TextBox slt = new TextBox();
32 slt.Height = 40;
33 slt.Width = 200;
34 slt.Text = ex.Message;
35 Canvas.SetLeft(slt, 20);
36 Canvas.SetTop(slt, 300);
37 this.LayoutRoot.Children.Add(slt);
38 }
39