none
テスト投稿 with HTML/C#/XAML from マイクロソフト RRS feed

  • General discussion

  • どんなものか、試してみます。

    HTMLページ

    1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    2 <html xmlns="http://www.w3.org/1999/xhtml" > 
    3 <!-- saved from url=(0014)about:internet --> 
    4 <head> 
    5     <title>HtmlBridge02</title> 
    6  
    7     <style type="text/css">  
    8     html, body {  
    9         height: 100%;  
    10         overflow: auto;  
    11     }  
    12     body {  
    13         padding: 0;  
    14         margin: 0;  
    15     }  
    16     #silverlightControlHost {  
    17         height: 100%;  
    18     }  
    19     </style> 
    20     <script type="text/javascript" src="Silverlight.js"></script> 
    21     <script type="text/javascript">  
    22         function onSilverlightError(sender, args) {  
    23           
    24             var appSource = "";  
    25             if (sender != null && sender != 0) {  
    26                 appSource = sender.getHost().Source;  
    27             }   
    28             var errorType = args.ErrorType;  
    29             var iErrorCode = args.ErrorCode;  
    30               
    31             var errMsg = "Unhandled Error in Silverlight 2 Application " +  appSource + "\n" ;  
    32  
    33             errMsg += "Code: "+ iErrorCode + "    \n";  
    34             errMsg += "Category: " + errorType + "       \n";  
    35             errMsg += "Message: " + args.ErrorMessage + "     \n";  
    36  
    37             if (errorType == "ParserError")  
    38             {  
    39                 errMsg += "File: " + args.xamlFile + "     \n";  
    40                 errMsg += "Line: " + args.lineNumber + "     \n";  
    41                 errMsg += "Position: " + args.charPosition + "     \n";  
    42             }  
    43             else if (errorType == "RuntimeError")  
    44             {             
    45                 if (args.lineNumber != 0)  
    46                 {  
    47                     errMsg += "Line: " + args.lineNumber + "     \n";  
    48                     errMsg += "Position: " +  args.charPosition + "     \n";  
    49                 }  
    50                 errMsg += "MethodName: " + args.methodName + "     \n";  
    51             }  
    52  
    53             throw new Error(errMsg);  
    54         }  
    55         function Button1_onclick() {  
    56             slControl = document.getElementById('SilverlightControl');  
    57  
    58             for (i = 0; i < 30; i++) {  
    59                 slControl.content.MySilverlightObject.AddMember(i, "Name" + i.toString());  
    60             }  
    61             slControl.content.MySilverlightObject.RefreshGrid();  
    62         }  
    63         function Button2_onclick() {  
    64             slControl = document.getElementById('SilverlightControl');  
    65             slControl.content.MySilverlightObject.ClearList();  
    66             slControl.content.MySilverlightObject.RefreshGrid();  
    67         }  
    68     </script> 
    69 </head> 
    70  
    71 <body> 
    72 <div id='htmlControl'>  
    73     <input id="Button1" type="button" value="グリッドの更新" onclick="return Button1_onclick()" /> 
    74     <input id="Button2" type="button" value="リストの消去"   onclick="return Button2_onclick()" /></div>  
    75  
    76     <!-- Runtime errors from Silverlight will be displayed here.  
    77     This will contain debugging information and should be removed or hidden when debugging is completed --> 
    78     <div id='errorLocation' style="font-size: small;color: Gray;"></div> 
    79  
    80     <div id="silverlightControlHost">  
    81         <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" id="SilverlightControl">  
    82             <param name="source" value="ClientBin/HtmlBridge02.xap"/>  
    83             <param name="onerror" value="onSilverlightError" /> 
    84             <param name="background" value="white" /> 
    85             <param name="minRuntimeVersion" value="2.0.31005.0" /> 
    86             <param name="autoUpgrade" value="true" /> 
    87             <href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">  
    88                 <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>  
    89             </a> 
    90         </object> 
    91         <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe> 
    92     </div> 
    93 </body> 
    94 </html> 
    95  

    C#

    1 using System;  
    2 using System.Collections.Generic;  
    3 using System.Linq;  
    4 using System.Net;  
    5 using System.Windows;  
    6 using System.Windows.Controls;  
    7 using System.Windows.Documents;  
    8 using System.Windows.Input;  
    9 using System.Windows.Media;  
    10 using System.Windows.Media.Animation;  
    11 using System.Windows.Shapes;  
    12 using System.Windows.Browser;  
    13  
    14 namespace HtmlBridge02  
    15 {  
    16     public partial class Page : UserControl  
    17     {  
    18         private List<MyItem> myList = new List<MyItem>();  
    19         public Page()  
    20         {  
    21             InitializeComponent();  
    22             this.Loaded += new RoutedEventHandler(Page_Loaded);  
    23         }  
    24  
    25         void Page_Loaded(object sender, RoutedEventArgs e)  
    26         {  
    27             HtmlPage.RegisterScriptableObject("MySilverlightObject"this);     
    28         }  
    29         [ScriptableMember]  
    30         public void AddMember(string Id, string Name)  
    31         {  
    32             MyItem newItem = new MyItem();  
    33             newItem.ID = Id;  
    34             newItem.Name = Name;  
    35             myList.Add(newItem);  
    36         }  
    37         [ScriptableMember]  
    38         public void ClearList()  
    39         {  
    40             myList.Clear();  
    41         }  
    42  
    43         [ScriptableMember]  
    44         public void RefreshGrid()  
    45         {  
    46             MyDataGrid.ItemsSource = null;  
    47             MyDataGrid.ItemsSource = myList;  
    48         }  
    49  
    50     }  
    51 }  
    52  

    XAML

    1 <UserControl x:Class="HtmlBridge02.Page" 
    2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    4     xmlns:dg="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
    5     Width="400" Height="300">  
    6     <Grid x:Name="LayoutRoot" Background="White">  
    7         <dg:DataGrid x:Name="MyDataGrid" AutoGenerateColumns="True" /> 
    8     </Grid> 
    9 </UserControl> 
    10  

     

    へぇー、これなら使いやすいかも。。。


    Microsoft
    Friday, January 23, 2009 2:38 PM