日期:2014-05-20  浏览次数:20813 次

wpf 到底错在哪了?
<Window
xmlns= 'http://schemas.microsoft.com/winfx/2006/xaml/presentation '
Title= 'Hello   World! '>
<Button> Hello   World! </Button>
</Window>

xaml   的文件内容如上,保存后在浏览器上会出错如下错误:

Startup   URI:   D:\EBOOK\WPF\Essential.WPF\HelloWorld.xaml
Application   Identity:   file:///C:/WINDOWS/Microsoft.Net/Framework/v3.0/WPF/XamlViewer/XamlViewer_v0300.xbap#XamlViewer_v0300.application,   Version=3.0.0.0,   Culture=neutral,   PublicKeyToken=c3bce3770c238a49,   processorArchitecture=msil/XamlViewer_v0300.exe,   Version=3.0.0.0,   Culture=neutral,   PublicKeyToken=c3bce3770c238a49,   processorArchitecture=msil,   type=win32

System.Security.SecurityException:   Request   for   the   permission   of   type   'System.Security.Permissions.SecurityPermission,   mscorlib,   Version=2.0.0.0,   Culture=neutral,   PublicKeyToken=b77a5c561934e089 '   failed.
      at   System.Security.CodeAccessSecurityEngine.Check(Object   demand,   StackCrawlMark&   stackMark,   Boolean   isPermSet)
      at   System.Security.CodeAccessPermission.Demand()
      at   MS.Internal.PresentationFramework.SecurityHelper.DemandUnmanagedCode()
      at   System.Windows.Window..ctor()
.....

不知道是怎么回事?


------解决方案--------------------
代码没问题 在XAMLPad上正常显示

System.Security.SecurityException
是.NET FRAMEWORK的SECURITY问题,设置为完全信任环境

加一段代码试试

using System.Security.Permissions;
[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name= "Internet ")]
[assembly: PermissionSetAttribute(SecurityAction.RequestOptional,Unrestricted=true)]
------解决方案--------------------
这是因为不管是松散的XAML文件,还是XBAP应用(WPF Browser Application),
如果是在浏览器(IE)中运行的化,都是运行在WPF Internet Sandbox沙箱中,
因而不具备创建窗口(Window)的权限,具体细节可以参考:
http://www.microsoft.com/china/msdn/library/security/wpfsecuritysandbox.mspx?mfr=true