关于WPF浏览器应用程序下的位图效果
2010-09-07 15:41
381 查看
看到MSDN上介绍WPF的Web浏览器应用程序有这样一段话:
“由于WPF既支持网络版应用程序,也支持安装版应用程序,因此桌面应用程序和Web应用程序可以共享相同的代码库。”。于是我新建WPF浏览器应用程序,复制之前写的WPF桌面应用程序的所有代码,把XAML中的<Window>改为<Page>,一运行却编译出错:
“无法创建在程序集“xxx(项目名),Version=1.0.0.0,Culture=neutral, PublicKeyToken=null”中定义的“Page1”的实例。调用的目标发生了异常。标记文件中“Page1.xaml”行x位置x处的错误。”
我很是不解,上网找了很久,发现遇到这个问题的人不少,但是居然都没找到合适的解决方案。最后还是在MSDN上,终于发现了问题的关键,原来“WPF位图效果不支持部分信任执行。若要使用位图效果,应用程序必须具有完全信任权限”。默认情况下,WPF的Web应用程序只具有部分信任权限。唉,看来以后遇到.NET的问题要先上MSDN看看!!
还搜到一篇不错的文章,利用这位Josh Smith写的扩展标记(http://joshsmithonwpf.wordpress.com/2008/06/12/writing-xaml-that-gracefully-degrades-in-partial-trust-scenarios/),在不完全信任的权限下,Web应用程序也能编译通过。应用程序可根据安全权限级别,选择是否给控件渲染位图特效。将应用程序的安全权限设为“这是完全可信的应用程序”,就能在Web应用程序中看到位图特效啦!
/// <summary>
/// 获取应用程序安全权限级别的扩展标记
/// </summary>
[ContentProperty("Xaml")]
public class IfFullTrustExtension : MarkupExtension
{
readonly static bool FullTrust;
static IfFullTrustExtension()
{
try
{
var state = PermissionState.Unrestricted;
new UIPermission(state).Assert();
FullTrust = true;
}
catch { }
}
public string Xaml { get; set; }
public override object ProvideValue(IServiceProvider sp)
{
object value = null;
if (FullTrust)
{
try
{
using (var str = new StringReader(Xaml))
using (var xml = XmlReader.Create(str))
value = XamlReader.Load(xml);
}
catch (Exception ex){ Debug.Fail("无效的XAML./r/n" + ex); }
}
return value;
}
}
前台Xaml代码: <Canvas Width="230" Background="Aliceblue">
<Button Width="170" Height="50" Margin="30,10,0,0" Background="#F5F5F0" Content="BevelBitmapEffect">
<Button.BitmapEffect>
<safe:IfFullTrust>
<![CDATA[
<BevelBitmapEffect xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
BevelWidth="13" />
]]>
</safe:IfFullTrust>
</Button.BitmapEffect>
</Button>
<Button Width="170" Height="30" Margin="30,70,0,0" Background="#F5F5F0" Content="DropShadowBitmapEffect">
<Button.BitmapEffect>
<safe:IfFullTrust>
<![CDATA[
<DropShadowBitmapEffect xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
ShadowDepth="20" Softness="0.7" Opacity="0.5" />
]]>
</safe:IfFullTrust>
</Button.BitmapEffect>
</Button>
<TextBox Width="170" Height="25" Margin="30,150,0,0" FontSize="15" Text="OuterGlowBitmapEffect">
<TextBox.BitmapEffect>
<safe:IfFullTrust>
<![CDATA[
<OuterGlowBitmapEffect xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
GlowColor="Blue" GlowSize="25" Noise="1" Opacity="0.6" />
]]>
</safe:IfFullTrust>
</TextBox.BitmapEffect>
</TextBox>
</Canvas>
完全信任权限下的效果图:
注:WPF位图效果以软件模式呈现。将位图效果应用于大型可视对象或动画会降低性能。因此在有大量Visual对象或动画时需慎用,要在界面效果与效率之间做出相对平衡。
“由于WPF既支持网络版应用程序,也支持安装版应用程序,因此桌面应用程序和Web应用程序可以共享相同的代码库。”。于是我新建WPF浏览器应用程序,复制之前写的WPF桌面应用程序的所有代码,把XAML中的<Window>改为<Page>,一运行却编译出错:
“无法创建在程序集“xxx(项目名),Version=1.0.0.0,Culture=neutral, PublicKeyToken=null”中定义的“Page1”的实例。调用的目标发生了异常。标记文件中“Page1.xaml”行x位置x处的错误。”
我很是不解,上网找了很久,发现遇到这个问题的人不少,但是居然都没找到合适的解决方案。最后还是在MSDN上,终于发现了问题的关键,原来“WPF位图效果不支持部分信任执行。若要使用位图效果,应用程序必须具有完全信任权限”。默认情况下,WPF的Web应用程序只具有部分信任权限。唉,看来以后遇到.NET的问题要先上MSDN看看!!
还搜到一篇不错的文章,利用这位Josh Smith写的扩展标记(http://joshsmithonwpf.wordpress.com/2008/06/12/writing-xaml-that-gracefully-degrades-in-partial-trust-scenarios/),在不完全信任的权限下,Web应用程序也能编译通过。应用程序可根据安全权限级别,选择是否给控件渲染位图特效。将应用程序的安全权限设为“这是完全可信的应用程序”,就能在Web应用程序中看到位图特效啦!
/// <summary>
/// 获取应用程序安全权限级别的扩展标记
/// </summary>
[ContentProperty("Xaml")]
public class IfFullTrustExtension : MarkupExtension
{
readonly static bool FullTrust;
static IfFullTrustExtension()
{
try
{
var state = PermissionState.Unrestricted;
new UIPermission(state).Assert();
FullTrust = true;
}
catch { }
}
public string Xaml { get; set; }
public override object ProvideValue(IServiceProvider sp)
{
object value = null;
if (FullTrust)
{
try
{
using (var str = new StringReader(Xaml))
using (var xml = XmlReader.Create(str))
value = XamlReader.Load(xml);
}
catch (Exception ex){ Debug.Fail("无效的XAML./r/n" + ex); }
}
return value;
}
}
前台Xaml代码: <Canvas Width="230" Background="Aliceblue">
<Button Width="170" Height="50" Margin="30,10,0,0" Background="#F5F5F0" Content="BevelBitmapEffect">
<Button.BitmapEffect>
<safe:IfFullTrust>
<![CDATA[
<BevelBitmapEffect xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
BevelWidth="13" />
]]>
</safe:IfFullTrust>
</Button.BitmapEffect>
</Button>
<Button Width="170" Height="30" Margin="30,70,0,0" Background="#F5F5F0" Content="DropShadowBitmapEffect">
<Button.BitmapEffect>
<safe:IfFullTrust>
<![CDATA[
<DropShadowBitmapEffect xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
ShadowDepth="20" Softness="0.7" Opacity="0.5" />
]]>
</safe:IfFullTrust>
</Button.BitmapEffect>
</Button>
<TextBox Width="170" Height="25" Margin="30,150,0,0" FontSize="15" Text="OuterGlowBitmapEffect">
<TextBox.BitmapEffect>
<safe:IfFullTrust>
<![CDATA[
<OuterGlowBitmapEffect xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
GlowColor="Blue" GlowSize="25" Noise="1" Opacity="0.6" />
]]>
</safe:IfFullTrust>
</TextBox.BitmapEffect>
</TextBox>
</Canvas>
完全信任权限下的效果图:
注:WPF位图效果以软件模式呈现。将位图效果应用于大型可视对象或动画会降低性能。因此在有大量Visual对象或动画时需慎用,要在界面效果与效率之间做出相对平衡。
相关文章推荐
- 关于css部份技巧, div在IE6/IE7/IE8/FF 不同浏览器 中的效果兼容
- wpf 浏览器应用程序去除系统自带的网页导航按钮
- 使用浏览器wpf应用程序时访问数据库需要报权限错误的解决方法
- 关于WPF里的一些Effect效果..
- 如何使Wpf浏览器应用程序被完全信任运行
- 疑难散笔-如何确保WPF浏览器应用程序运行成功-很多新手肯定会遇到的问题
- 关于浏览器和网络的20项须知-网络应用程序
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- 关于 WebBrowser只对浏览器外应用程序以及在Internet Explorer 中以提升权限运行的应用程序启用
- wpf界面展示关于ListView展示有倒影效果
- WPF浏览器应用程序
- weblogic部署应用程序,应用程序修改JSP页面浏览器刷新无效果解决办法
- 一步一步部署WPF浏览器应用程序
- 一步一步部署WPF浏览器应用程序【原创】
- WPF,Silverlight与XAML读书笔记第十一 - WPF XAML浏览器应用程序介绍
- 关于css的reset:统一浏览器显示效果
- 学习WPF动画效果关于resources key storyboard name
- WPF 关于控件背景色的设置和穿透效果
- WPF 关于控件背景色的设置和穿透效果
- WPF浏览器应用程序与JS的互调用(不用WebBrowser)