您的位置:首页 > 其它

关于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对象或动画时需慎用,要在界面效果与效率之间做出相对平衡。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐