稳扎稳打Silverlight(67) - 5.0被信任的应用程序之访问本地文件系统, 支持多窗口, 被信任的程序在浏览器中运行, Call Windows API

[索引页]
[源码下载]


稳扎稳打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
[源码下载]

posted @ 2012-03-22 08:31  webabcd  阅读(5762)  评论(11编辑  收藏  举报