.NET TIPS
[Silverlight 2]コードにより別ウィンドウを開くには?[C#、VB]
デジタルアドバンテージ 遠藤 孝信
2009/12/17
「TIPS:コードによりページを遷移するには? 」で紹介しているように、HtmlWindowクラス(System.Windows.Browser名前空間)のNavigateメソッド では、第2引数に「_blank」を指定することにより別ウィンドウで指定したページを開くことができる。
しかしこの場合、例えばIE 8のタブ設定で「常に新しいタブでポップアップを開く」という設定にしていると、(別ウィンドウはタブとして開くが)新しく開いたタブがアクティブにならない(元のウィンドウが表示されたままとなる)。
Silverlightから別ウィンドウを開く手段としては、HtmlPageクラス(System.Windows.Browser名前空間)にPopupWindowメソッド というメソッドも用意されており、こちらのメソッドでは、IE 8でウィンドウをタブとして開く場合でも、新しく開いたタブがアクティブになる。
PopupWindowメソッドの呼び出し例は次のようになる(実際のコード記述例は後述)。
HtmlPage.PopupWindow(linkUri, "_blank", null);
HtmlPage.PopupWindow(linkUri, "_blank", Nothing)
PopupWindowメソッドの呼び出し例(上:C#、下:VB)
メソッドの第1、第2引数はNavigateメソッドと同じで、遷移先のUriクラス(System名前空間)のオブジェクトと、遷移先のページを開くウィンドウ(あるいはフレーム)を指定する。
第3引数には、HtmlPopupWindowOptionsクラス(System.Windows.Browser名前空間) のインスタンスにより、開くポップアップ・ウィンドウのさまざまなオプションを指定できるが、null/Nothingの場合はブラウザの既定値が使用される。
PopupWindowメソッドを使用したサンプル・アプリケーション
次のSilverlightコントロールは、NavigateメソッドとPopupWindowメソッドでの挙動の違いを試すためのものだ。ボタンをクリックすると、それぞれのメソッドを使用して、Googleページを開く。
Silverlight 2のサンプル・アプリケーション
実際にボタンをクリックして試すことができる。
このアプリケーションのVisual Studio 2008プロジェクト(プロジェクト名は「PopupWindowSample」)内の、MainPage.xamlとMainPage.xaml.cs/MainPage.xaml.vbの内容を以下に示す。
<UserControl x:Class="PopupWindowSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot">
<StackPanel>
<Button Margin="10" Height="130" Click="Button_Click1"
Content='HtmlPage.Window.Navigate(linkUri, "_blank")'/>
<Button Margin="10" Height="130" Click="Button_Click2"
Content='HtmlPage.PopupWindow(linkUri, "_blank", null)'/>
</StackPanel>
</Grid>
</UserControl>
Page.xaml
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Browser;
namespace PopupWindowSample
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
Uri linkUri = new Uri("http://www.google.co.jp");
private void Button_Click1(object sender, RoutedEventArgs e)
{
HtmlPage.Window.Navigate(linkUri, "_blank");
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
HtmlPage.PopupWindow(linkUri, "_blank", null);
}
}
}
Imports System.Windows.Browser
Partial Public Class MainPage
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Dim linkUri As New Uri("http://www.google.co.jp")
Private Sub Button_Click1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
HtmlPage.Window.Navigate(linkUri, "_blank")
End Sub
Private Sub Button_Click2(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
HtmlPage.PopupWindow(linkUri, "_blank", New HtmlPopupWindowOptions())
End Sub
End Class
上:MainPage.xaml.cs、下:MainPage.xaml.vb
なお、PopupWindowメソッドで第3引数にHtmlPopupWindowOptionsオブジェクトを指定した場合には、開くウィンドウのサイズが自動的に一回り小さく設定される点に注意してほしい。これについては、MSDNのPopupWindowメソッド の項に記されている。
カテゴリ: Silverlight 2 処理対象: ブラウザ
使用ライブラリ: HtmlWindowクラス(System.Windows.Browser名前空間)
使用ライブラリ: HtmlPageクラス(System.Windows.Browser名前空間)
使用ライブラリ: Uriクラス(System名前空間)
関連TIPS: コードによりページを遷移するには?
generated by
Insider.NET 記事ランキング
本日
月間