IronPythonで、Loose XAMLファイルを実行する。
Google Chrome12 では、読み込むとエラーになるので、作ってみました。
LooseXaml2.py: ローカルファイルのLooseXAMLをIronPythonで、読み込み実行。
LooseXaml3.py: インターネットにあるLooseXAMLをIronPythonで、読み込み実行。
https://github.com/softgarden137/LooseXAML
Google Chrome12 では、読み込むとエラーになるので、作ってみました。
Loose XAML⇒DropShadowInk.xaml ⇒ Drop-shadows are cool!
LooseXaml.py: LoosXAMLをIronPythonで、ラップしました。LooseXaml2.py: ローカルファイルのLooseXAMLをIronPythonで、読み込み実行。
LooseXaml3.py: インターネットにあるLooseXAMLをIronPythonで、読み込み実行。
#
# LooseXaml.py
# ipy.exe LooseXaml.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
xaml_str="""
<Page WindowTitle = "Binding ( Slider, Rectangle )"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid
Background="OrangeRed" Width="300" Height="300" >
<Slider x:Name = "MySlider"
VerticalAlignment = "Top"
Minimum = "10"
Maximum = "200"
Value = "50"
Margin = "10" />
<Rectangle
Width = "{Binding ElementName = MySlider, Path = Value}"
Height = "{Binding ElementName = MySlider, Path = Value}"
Fill = "Orange"
VerticalAlignment = "Center"
HorizontalAlignment = "Center" />
</Grid>
</Page>
"""
class Window1(Window):
def __init__(self):
self.Content = XamlReader.Parse(xaml_str)
if __name__ == "__main__":
win = Window1()
Application().Run(win)
# LooseXaml.py
# ipy.exe LooseXaml.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
xaml_str="""
<Page WindowTitle = "Binding ( Slider, Rectangle )"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid
Background="OrangeRed" Width="300" Height="300" >
<Slider x:Name = "MySlider"
VerticalAlignment = "Top"
Minimum = "10"
Maximum = "200"
Value = "50"
Margin = "10" />
<Rectangle
Width = "{Binding ElementName = MySlider, Path = Value}"
Height = "{Binding ElementName = MySlider, Path = Value}"
Fill = "Orange"
VerticalAlignment = "Center"
HorizontalAlignment = "Center" />
</Grid>
</Page>
"""
class Window1(Window):
def __init__(self):
self.Content = XamlReader.Parse(xaml_str)
if __name__ == "__main__":
win = Window1()
Application().Run(win)
#
# LooseXaml2.py
# ipy.exe LooseXaml2.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
def LoadXaml(filename):
from System.IO import *
from System.Windows.Markup import XamlReader
f = FileStream(filename, FileMode.Open, FileAccess.Read)
try:
element = XamlReader.Load(f)
finally:
f.Close()
return element
class Window1(Window):
def __init__(self):
self.Content = LoadXaml("LooseXaml.xaml")
if __name__ == "__main__":
win = Window1()
Application().Run(win)
# LooseXaml2.py
# ipy.exe LooseXaml2.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
def LoadXaml(filename):
from System.IO import *
from System.Windows.Markup import XamlReader
f = FileStream(filename, FileMode.Open, FileAccess.Read)
try:
element = XamlReader.Load(f)
finally:
f.Close()
return element
class Window1(Window):
def __init__(self):
self.Content = LoadXaml("LooseXaml.xaml")
if __name__ == "__main__":
win = Window1()
Application().Run(win)
#
# LooseXaml3.py
# ipy.exe LooseXaml3.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System import Uri, UriKind
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
def LoadXaml3(strUrl):
import System
# Create a request for the URL.
request = System.Net.WebRequest.Create(strUrl)
# If required by the server, set the credentials.
#request.Credentials = System.Net.CredentialCache.DefaultCredentials
# Get the response.
response = request.GetResponse()
# Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
try:
element = XamlReader.Load(dataStream)
finally:
# Cleanup the streams and the response.
dataStream.Close()
response.Close()
return element
class Window1(Window):
def __init__(self):
self.Content = LoadXaml3("http://softgarden.lovepop.jp/myBlog/xaml/dropshadowink.xaml")
if __name__ == "__main__":
win = Window1()
Application().Run(win)
GitHubにプログラムを投稿しています。参考にしてください。# LooseXaml3.py
# ipy.exe LooseXaml3.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System import Uri, UriKind
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
def LoadXaml3(strUrl):
import System
# Create a request for the URL.
request = System.Net.WebRequest.Create(strUrl)
# If required by the server, set the credentials.
#request.Credentials = System.Net.CredentialCache.DefaultCredentials
# Get the response.
response = request.GetResponse()
# Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
try:
element = XamlReader.Load(dataStream)
finally:
# Cleanup the streams and the response.
dataStream.Close()
response.Close()
return element
class Window1(Window):
def __init__(self):
self.Content = LoadXaml3("http://softgarden.lovepop.jp/myBlog/xaml/dropshadowink.xaml")
if __name__ == "__main__":
win = Window1()
Application().Run(win)

![]() | IronPythonの世界 (Windows Script Programming) |
荒井 省三 | |
ソフトバンク クリエイティブ |
![]() | エキスパートPythonプログラミング |
Tarek Ziade | |
アスキー・メディアワークス |
![]() | Pythonスタートブック |
辻 真吾 | |
技術評論社 |