A colleague at DevelopMentor recently asked me about creating popup windows in XAML browser applications (XBAP). Normally this is not allowed – if you try to create a top-level window you will get a SecurityException because WPF asks forUIPermission which is strictly prohibited when hosted in the browser.

It turns out, however, that you can get a popup window – there’s a hidden little gem in theSystem.Windows.Controls.Primitive namespace that is your friend: Popup.

It’s the same underlying class that ToolTip, Menu, and ComboBox use to display drop-down menus and overlays and it is browser-hosting aware! It’s pretty limited in functionality – I’m not sure you can get it to move around with the mouse for example, but for simple cases it works great. Here’s a code snippet – wire this up to a button in an XBAP:

void OnClick(object sender, EventArgs e)
{
Popup window = new Popup(); StackPanel sp = new StackPanel { Margin = new Thickness() };
sp.Children.Add(new TextBlock { Text = "Hi from a popup" });
Button newButton = new Button { Content = "Another button" };
newButton.Click += delegate { window.IsOpen = false; };
sp.Children.Add(newButton);
sp.Children.Add(new Slider { Minimum = , Maximum = , Value = , Width = }); window.Child = new Border { Background = Brushes.White, BorderBrush = Brushes.Black,
BorderThickness = new Thickness(), Child = sp };
window.PlacementTarget = this;
window.Placement = PlacementMode.Center; window.IsOpen = true;
}

The key thing you need to do is set the PlacementTarget. That associates a “parent” window and without it, the Popup class asserts UIPermission which will fail in the browser environment.

Creating popup windows in XBAP applications的更多相关文章

  1. Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

    创建基于对话框的Windows应用程序(四)—— Edit Control.Combo Box的应用.Unicode转ANSI.Open File Dialog.文件读取.可变参数.自动滚动 之前的介 ...

  2. Creating Dialogbased Windows Application (3) / 创建基于对话框的Windows应用程序(三)Checkbox的应用、窗体置顶、设置图标 / VC++, Windows

    创建基于对话框的Windows应用程序(三) —— Checkbox的应用.窗体置顶.设置图标 上一节创建的窗体应用程序中,我们用到了Button和StaticText这两个控件.这一节中我们将学习使 ...

  3. Creating Dialogbased Windows Application (2) / 创建基于对话框的Windows应用程序(二)Button的应用、新建子窗体 / VC++, Windows

    创建基于对话框的Windows应用程序(二) —— Button的应用.新建子窗体 可以发现上一节创建的窗体中,点击OK和Cancel两个按钮是没有任何反应的.现在我们来为他们添加退出对话框的功能. ...

  4. Creating Dialogbased Windows Application (1) / 创建基于对话框的Windows应用程序(一)新建窗体 / VC++, Windows

    创建基于对话框的Windows应用程序(一) —— 新建窗体 1.新建一个Visual C++的Empty Project.  2.在Solution Explorer中右键Add New Item, ...

  5. System Operations on AWS - Lab 1W - Creating EC2 (Windows)

    1. 创建CommandHost实例,登录到CommandHost,通过AWS CLI创建WebServer实例. 1.1 为CommandHost实例创建一个IAM角色 1.2 创建CommandH ...

  6. windows消息机制详解(转载)

    消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...

  7. Receive Windows Messages In Your Custom Delphi Class - NonWindowed Control - AllocateHWnd

    http://delphi.about.com/od/windowsshellapi/a/receive-windows-messages-in-custom-delphi-class-nonwind ...

  8. windows消息处理(强烈推荐,收藏)

    由于看了一下,比较好理解,暂时先放到这里,待有空再翻译.只是在每节后大致介绍一下讲的内容. 感觉写的比较全,无论从消息的原理还是从MFC操作上来说,值得一看,我也在此做个收藏. (一) 说明:以下首先 ...

  9. Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services

    本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Servi ...

随机推荐

  1. linux修改文件打开最大数(ulimit命令)

    解除 Linux 系统的最大进程数和最大文件打开数限制:vi /etc/security/limits.conf# 添加如下的行* soft noproc 65536 * hard noproc 65 ...

  2. [转]如何取得当前正在执行的shell脚本的绝对路径?

    来源:http://sexywp.com/bash-how-to-get-the-basepath-of-current-running-script.htm 如题,一般我们写Shell脚本的时候,都 ...

  3. Eclipse的git插件冲突合并方法

    Eclipse有一个git的插件叫EGit,用于实现本地代码和远程代码对比.合并以及提交.但是在本地代码和远程代码有冲突的时候,EGit的处理方案还是有点复杂.今天就彻底把这些步骤给理清楚,并公开让一 ...

  4. Kali Linux上安装SSH服务

    安装 SSH 从终端使用 apt-get 命令安装 SSH 包: # apt-get update # apt-get install ssh 启用和开始使用 SSH 为了确保安全 shell 能够使 ...

  5. dubbo启动报错failed to bind nettyserver on

    dubbo报错 今天启动项目的时候,关掉了custom服务, <dubbo:consumer check="false"/> 并且关掉了spring的elastic-j ...

  6. 测试开发之前端——No1.HTML和HTML5

    学习之前,让我们先来了解一下HTML. 它的英文全称是:Hyper Text Markup Language,中文通常被称为超文本标记语言,HTML是Internet中用于编写网页的主要语言,HTML ...

  7. Jenkins 发布.NetCore 项目

    安装最新Jenkins及安装好相关git插件 启动jenkins服务,访问8080端口 这里就发布一个IdentityServer4程序 配置相关参数 设置Git源码管理配置 构建执行window 批 ...

  8. 【AtCoder】ARC093

    C - Traveling Plan 相当于一个环,每次删掉i点到两边的距离,加上新相邻的两个点的距离 代码 #include <bits/stdc++.h> #define fi fir ...

  9. H5中canvas标签制作在线画板

    1.介绍 左键点击下移动开始画图.放开鼠标不在画图. 2.重要使用理论 query的使用: 鼠标的按下mousedown 鼠标的移动mousemove 鼠标的放开 mouseup 3.程序 <! ...

  10. [C编码笔记] 空串与NULL是不一样的

    int main() { char *str = NULL; printf("%p \n", str); printf("%p \n", &str); ...