代码改变世界

silverlight系列(子窗口创建、引用webservice)

2010-03-17 12:17  key_sky  阅读(987)  评论(0编辑  收藏  举报

一、ChildWindow:

ChildWindow相关部件

  • Chrome:围绕在子窗口内容四周的框架
  • CloseButton:子窗口标题中的关闭按钮
  • ContentRoot:弹出窗口的根,包括Chrome和ContentPresenter
  • Overlay:在子窗口打开时覆盖父窗口的控件部件
  • Root:控件的根

MainPage.cs:

[Category("ChildWindow")]
private Childwindow childwindow;

public MainPage()
{
InitializeComponent();
childwindow
= new Childwindow();
childwindow.Closed
+= new EventHandler(window_Closed);

}

private void window_Closed(object sender, EventArgs e)
{
if ((bool)childwindow.DialogResult)
{
if (childwindow.Title.ToString().Trim() == "添加新文字元素")
{
CreateWordElement(childwindow.tbTextInfo.Text.Trim());
}
else
{
CreatePicElement(childwindow.lbxPic.SelectedItem.ToString());
}
}
}

/// <summary>
/// 生成文字元素
/// </summary>
/// <param name="strtext"></param>
private void CreateWordElement(string strtext)
{
}

/// <summary>
/// 生成图片元素
/// </summary>
/// <param name="strpic"></param>
private void CreatePicElement(string strpic)
{
}
private void btnAddElement_Click(object sender, RoutedEventArgs e)
{
childwindow.Title
= "添加新图片元素";
childwindow.Show();
childwindow.tbtype.Text
= "选择图片:";
childwindow.lbxPic.Visibility
= Visibility.Visible;
childwindow.lbxPic.ItemsSource
= listPic;
childwindow.tbTextInfo.Visibility
= Visibility.Collapsed;
childwindow.lbxPic.SelectedIndex
= 0;
}

ChildWindow.xaml:

<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentControl HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding}" />
<StackPanel Grid.Row="0" x:Name="optionsStack">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="遮盖区域透明度: " Margin="4" HorizontalAlignment="Stretch" />
<Slider Grid.Column="1" Minimum="0" IsEnabled="True" Maximum="1"
Value
="{Binding OverlayOpacity, Mode=TwoWay}" HorizontalAlignment="Stretch" />
</Grid>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text=" 遮盖区域颜色: " Margin="4" />
<ComboBox SelectedItem="{Binding OverlayBrush, Mode=TwoWay}" Width="200"
Grid.Column
="1" IsEnabled="True" HorizontalAlignment="Stretch">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="2" Text="{Binding Color}" FontFamily="Consolas"
VerticalAlignment
="Center" />
<Rectangle Fill="{Binding}" Margin="2" Stroke="Black" Height="14"
Width
="75" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<SolidColorBrush Color="White" Opacity=".7" />
<SolidColorBrush Color="Black" Opacity=".7" />
<SolidColorBrush Color="Blue" Opacity=".7" />
<SolidColorBrush Color="Yellow" Opacity=".7" />
<SolidColorBrush Color="Pink" Opacity=".7" />
<SolidColorBrush Color="Orange" Opacity=".7" />
<SolidColorBrush Color="Green" Opacity=".7" />
<SolidColorBrush Color="Red" Opacity=".7" />
</ComboBox>
</Grid>

<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="填充文字: " Margin="10" HorizontalAlignment="Stretch" x:Name="tbtype"/>
<TextBox Grid.Column="1" Margin="10" HorizontalAlignment="Stretch" x:Name="tbTextInfo"
GotFocus
="tbTextInfo_GotFocus"></TextBox>

<ListBox x:Name="lbxPic" HorizontalAlignment="Stretch" Height="80"
Grid.Column
="1" Margin="10"></ListBox>

</Grid>

</StackPanel>
<Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="23"
HorizontalAlignment
="Left" Margin="-200,0,0,0" Grid.Row="1" Grid.Column="1"
VerticalAlignment
="Bottom"/>
<Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="23"
HorizontalAlignment
="Left" Grid.Row="1" Grid.Column="0"
VerticalAlignment
="Bottom"/>
<!--
ChildWindow相关部件
Chrome:围绕在子窗口内容四周的框架
CloseButton:子窗口标题中的关闭按钮
ContentRoot:弹出窗口的根,包括Chrome和ContentPresenter
Overlay:在子窗口打开时覆盖父窗口的控件部件
Root:控件的根
-->
</Grid>

ChildWindow.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DragElement
{
public partial class Childwindow : ChildWindow
{
public Childwindow()
{
InitializeComponent();
optionsStack.DataContext
= this;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Performance",
"CA1811:AvoidUncalledPrivateCode",Justification = "Used by event defined in Xaml.")]
//取消报告特定的静态分析工具规则冲突,允许一个代码项目上应用多个取消报告设置
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Performance",
"CA1811:AvoidUncalledPrivateCode", Justification = "Used by event defined in Xaml.")]
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}

private void tbTextInfo_GotFocus(object sender, RoutedEventArgs e)
{

}
}
}

运行效果:

点击按钮新添加图片元素:

 

点击确定按钮:

二、调用webservice

1.创建webservice:

SaveXml.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DragElement.Web
{
/// <summary>
/// SaveXml 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(
false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class SaveXml : System.Web.Services.WebService
{

[WebMethod]
public bool SavemyXml(string str)
{
try
{
string UpdateSQL = "update flow set xmltext='" + str + "' where flowname='设计器1'";
SqlConnection conn
= new SqlConnection(ConfigurationManager.
ConnectionStrings[
"flowdata"].ConnectionString);
SqlCommand comm
= new SqlCommand(UpdateSQL, conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
return true;
}
catch
{
return false;
}
}
}
}

2.给web添加固定端口

 

3.给silverlight项目添加服务引用