Using the Modal Child WIndow in SL 4.0

创建注册表单并实现模态窗口确认流程
本文指导如何在Silverlight应用中创建一个包含注册表单和模态窗口确认流程的简单应用程序,包括布局设计、XAML代码编写以及事件处理。

In this exercise, you will create a simple registration form that accepts a first and last name. When someone presses the button to register, a modal window will appear with a terms and conditions notice that users must agree to before proceeding. You won't fully code the registration form, you'll just send a result to a TextBlock so you can see what's going on. Let's get started.

1. Create a new SL application in VS 2010 called ModalWindow. Allow VS to create a web application to host the application.

2. In the MainPage.Xaml file, divide the root Grid into five rows and two columns. The height of the first four rows should be 40px, and the fifth row should take up the remainder of the application. The width of the first column should be 150 px, and the second should take up the remainder of the application. In addition, change the d:DesignWidth of the UserControl to 600.

3. In the first row, add a Textblock for a header with the Text "Register for a new Account" that spans both columns. In the Second row, add a TextBlock in the first column with the Text "First Name", and add a TextBox in the second column. Add some Margin and Padding to improve the appearance.

4. In the third row, add another TextBlock in the first column with the Text "Last Name", and add a textBox in the second column. Add some Margin and Padding to improve the appearance. In the fourth row, add a Button to the second column with the text "Register". finally, in the fifth row, add a TextBlock to the second column with the Text blank. Name the TextBlock "Result". Your XAML should look like the following code.

5. Now that you have the main form laid out, turn your attention to the child window. To add a child window to the project, right-cick on the silverlight project and select add new item. From the add new item dialog, select sl child window andname the window Confirm.xaml.

6. when the child window has been added to the project, it will contain the following xaml by default.

7. modify the child window xaml file code, like following code:

<Grid x:Name="LayoutRoot" Margin="2">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
 
    <StackPanel>
 
        <TextBlock 
            Text="Please Accept the Terms and Conditions to Continue"
            FontWeight="Bold" 
            FontSize="12" />
 
        <TextBlock 
            Text="These are the terms and conditions..." />
 
    </StackPanel>
 <Button 
        x:Name="CancelButton"
        Content="I Do Not Accept" Click="CancelButton_Click" 
        Width="125" 
        Height="23" HorizontalAlignment="Right" 
        Margin="0,12,0,0" Grid.Row="1" />
 
    <Button 
        x:Name="OKButton" 
        Content="I Accept" Click="OKButton_Click" 
        Width="100" 
        Height="23" HorizontalAlignment="Right" 
        Margin="0,12,134,0" Grid.Row="1" />
 
</Grid>

9.Go ahead and run the application again and then press the Register button to open the Child Window. Notice that the content changes are reflected.Keep in mind that the content of these window controls is completely customizable with XAML. You can add any controls you wish with any layout you want.

10.Now let’s add code to retrieve results from the dialog. Open the MainPage.xaml.cs file and within the Button_Click event handler, wire up another event handler for the Child Window’s Closed() event. In this new event handler, you need to get the Child Window’s instance, which is sent to the handler in the sender object. Once you have the window’s instance, you can retrieve the DialogResult property, which will contain true, false, or null. 

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Confirm confirmDlg = new Confirm();
            confirmDlg.Closed += new EventHandler(confirmDlg_Closed);
            confirmDlg.Show();
        }

        void confirmDlg_Closed(object sender, EventArgs e)
        {
            Confirm confirmDlg = (Confirm)sender;
            if (confirmDlg.DialogResult == true)
                this.Result.Text = "terms and Conditions Accepted.";
            else
                this.Result.Text = "terms and Conditions Not Accepted.";
        }

11. Run the application.

转载于:https://www.cnblogs.com/jerrychenfly/archive/2011/08/15/2139633.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值