穩扎穩打Silverlight(67) - 5.0被信任的應用程序之訪問本地文件系統, 支持多窗口, 被信任的程序在瀏覽器中運行, Call Windows API
作者:webabcd
介紹
Silverlight 5.0 被信任的應用程
- AccessFileSystem - 訪問本地文件系統
- MultipleWindows - 支持多窗口
- In Browser - 被信任的程序在瀏覽器中運行
- Call Windows API
在線DEMO
http://www.cnblogs.com/webabcd/archive/2012/03/05/2379862.html
示例
1、AccessFileSystem(訪問本地文件系統)
TrustedApplication/AccessFileSystem.xaml
<navigation:Page x:Class="Silverlight50.TrustedApplication.AccessFileSystem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="AccessFileSystem Page">
<StackPanel x:Name="LayoutRoot" HorizontalAlignment="Left">
<TextBlock Text="在不使用 OpenFileDialog 和 SaveFileDialog 的情況下,訪問本地文件系統" />
<!-- 顯示“C盤”中的文件夾列表 -->
<ListBox Name="listBoxDirectory" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="1" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- 顯示“C盤”中的文件列表 -->
<ListBox Name="listBoxFile" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="1" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</navigation:Page>
TrustedApplication/AccessFileSystem.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO;
namespace Silverlight50.TrustedApplication
{
public partial class AccessFileSystem : Page
{
public AccessFileSystem()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.Current.IsRunningOutOfBrowser)
{
MessageBox.Show("請在OOB中運行");
LayoutRoot.Visibility = Visibility.Collapsed;
}
// Application.Current.HasElevatedPermissions - 判斷 OOB 模式中的應用程序是否為被信任的應用程序
if (Application.Current.HasElevatedPermissions)
{
// 獲取 C 盤的目錄信息
DirectoryInfo root = new DirectoryInfo(@"C:\");
listBoxDirectory.ItemsSource = root.EnumerateDirectories();
listBoxFile.ItemsSource = root.EnumerateFiles();
}
}
}
}
2、MultipleWindows(支持多窗口)
TrustedApplication/MultipleWindows.xaml
<navigation:Page x:Class="Silverlight50.TrustedApplication.MultipleWindows"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="MultipleWindows Page">
<Grid x:Name="LayoutRoot" Background="White">
<Button Name="btn" Content="新開窗口" Click="btn_Click" />
</Grid>
</navigation:Page>
TrustedApplication/MultipleWindows.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight50.TrustedApplication
{
/// <summary>
/// 演示對多窗口的支持
/// </summary>
public partial class MultipleWindows : Page
{
Random _random = new Random();
public MultipleWindows()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.Current.IsRunningOutOfBrowser)
{
MessageBox.Show("請在OOB中運行");
LayoutRoot.Visibility = Visibility.Collapsed;
}
}
private void btn_Click(object sender, RoutedEventArgs e)
{
// Application.Current.HasElevatedPermissions - 判斷 OOB 模式中的應用程序是否為被信任的應用程序
if (Application.Current.HasElevatedPermissions)
{
/*
* Window - OOB 模式下的應用程序窗口
* Height - 窗口的高
* Width - 窗口的寬
* Top - 窗口距頂部的距離
* Left - 窗口距左側的距離
* Title - 窗口的標題
* Visibility - 窗口的可見性
* Content - 窗口的內容
* TopMost - 窗口是否顯示在最前面
* IsActive - 是否為激活窗口(僅 get)
* IsVisible - 窗口是否可見(僅 get)
* WindowState - 窗口的狀態(WindowState.Normal,WindowState.Minimized,WindowState.Maximized)
* WindowStyle - 窗口的樣式(WindowStyle.None,WindowStyle.SingleBorderWindow,WindowStyle.BorderlessRoundCornersWindow)
*
* Activate() - 激活窗口,使之具有焦點,並置於最前面
* Window.Close() - 關閉窗口
*
* Closing - 窗口關閉前觸發的事件
*/
Window newWindow = new Window();
newWindow.Height = 400;
newWindow.Width = 500;
newWindow.Top = 1 + _random.Next(0, 100);
newWindow.Left = 1 + _random.Next(0, 100);
newWindow.Title = "新窗口";
newWindow.Visibility = Visibility.Visible;
// 添加一個內部有按鈕的 Canvas,設置 Canvas 的背景色為白色
Button btn = new Button();
btn.Width = 80.0;
btn.Height = 30.0;
btn.Content = "點 擊";
btn.Margin = new Thickness(5, 10, 0, 0);
btn.Click += new RoutedEventHandler(btn_Click);
Canvas canvas = new Canvas();
canvas.Children.Add(btn);
canvas.Background = new SolidColorBrush(Colors.White);
newWindow.Content = canvas;
newWindow.WindowState = WindowState.Normal;
}
}
}
}
3、In Browser(被信任的程序在瀏覽器中運行)
TrustedApplication/InBrowser.xaml
<navigation:Page x:Class="Silverlight50.TrustedApplication.InBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="InBrowser Page">
<StackPanel x:Name="LayoutRoot" Orientation="Vertical" HorizontalAlignment="Left">
<HyperlinkButton TargetName="_blank" NavigateUri="http://msdn.microsoft.com/en-us/library/gg192793(v=vs.96).aspx" Content="如何使在瀏覽器中運行的程序成為被信任的程序(需要管理員介入)" />
</StackPanel>
</navigation:Page>
4、Call Windows API
TrustedApplication/CallWindowsAPI.xaml
<navigation:Page x:Class="Silverlight50.TrustedApplication.CallWindowsAPI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="CallWindowsAPI Page">
<StackPanel x:Name="LayoutRoot">
<Button Content="C 盤的驅動器類型" Click="Button_Click" />
</StackPanel>
</navigation:Page>
TrustedApplication/CallWindowsAPI.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Runtime.InteropServices;
namespace Silverlight50.TrustedApplication
{
public partial class CallWindowsAPI : Page
{
public CallWindowsAPI()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.Current.IsRunningOutOfBrowser)
{
MessageBox.Show("請在OOB中運行");
LayoutRoot.Visibility = Visibility.Collapsed;
}
}
// 演示如何調用 Windows API
[DllImport("kernel32.dll")]
static extern int GetDriveType(string lpRootPathName);
private void Button_Click(object sender, RoutedEventArgs e)
{
// Application.Current.HasElevatedPermissions - 判斷 OOB 模式中的應用程序是否為被信任的應用程序
if (Application.Current.HasElevatedPermissions)
{
MessageBox.Show("C 盤的驅動器類型:" + GetDriveType(@"c:\"));
}
}
// 注:非托管代碼回調托管代碼時,托管代碼應加上 [AllowReversePInvokeCalls] 標記
}
}
OK
[源碼下載]
