silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

 part1: 如何使用RIA services part2: RIA services更新和验证 part3:rIA services数据新增 part4:皮肤的更改 part5:报表的展示 part6:Endpoint的设置 part7:如何使用RIA service Class Library  part8:url重写和界面友好 1.       Custom Dataform用来新增一条记
@H_262_14@
@H_262_14@

 part1: 如何使用RIA Services

part2: RIA Services更新和验证

part3:RIA Services数据新增

part4:皮肤的更改

part5:报表的展示

part6:Endpoint的设置

part7:如何使用RIA Service Class Library 

part8:url重写和界面友好

1.       Custom Dataform用来新增一条记录。

前面那篇文章已经提到使用自定义DataForm来新增记录。可能你会觉得自定义DataForm干嘛,直接使用TextBlockTextBox就行。

自定义一个Dataform可以让数据的验证更容易的实现。

A.      先定义一个CustomRestaurant类,它继承了那面这两个接口。

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

下面那么多属性就是我们需要填写的所有字段。

然后我们添加一个验证规则是Name字段必须不为空且大于4个字符串。

    public String this[String columnName]

        {

            get

            {

                String result = null;

 

                if (columnName == "Name")

                {

                    if (String.IsNullOrEmpty(Name))

                        result = "Firstname has to be set!";

                    else if (Name.Length < 3)

                        result = "Firstname's length has to be at least 5 characters!";

                }

              

 

                return result;

          

 

 

B.      创建一个自定义控件,它的结构如下:

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

这里使用了两个DependencyProperty,一个是是否编辑,第二个是让TheRestaurant作为它的一个属性

我们这里使用resource Dictionary来存放模板信息:

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

定义DataForm的模板如下:

<Style TargetType="local:CusRestuarantDataForm">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="local:CusRestuarantDataForm">

                    <Border BACkground="{TemplateBinding BACkground}"

                            BorderBrush="{TemplateBinding BorderBrush}"

                            BorderThickness="{TemplateBinding BorderThickness}"

                            CornerRadius="3" Width="{TemplateBinding Width}">

                        <Grid>

                            <Grid.RowDeFinitions>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                                <RowDeFinition Height="Auto"/>

                               

                            </Grid.RowDeFinitions>

                            <Grid.columnDeFinitions>

                                <columnDeFinition Width="150"/>

                                <columnDeFinition Width="*"/>

                            </Grid.columnDeFinitions>

 

                            <TextBlock Grid.column="0" Grid.Row="0"

                    Text="Address:" Style="{Staticresource tb}" />

                            <TextBox Grid.column="1" Grid.Row="0"                           x:Name="tAddress" Style="{Staticresource tx}"

    Text="{Binding Path=TheRestaurant.Address, Mode=TwoWay, ValidatesOnDataErrors=TruE}" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="1"

                    Text="Code:" Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="1"                           x:Name="tCode" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.Code, ValidatesOnDataErrors=TruE}" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="2"                             Text="ContactName:" Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="2"                           x:Name="tContactName" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.ContactName, Mode=TwoWay}" IsReadOnly="{Binding IsLocked}" />

 

                            <TextBlock Grid.column="0" Grid.Row="3" Text="Contacttitle:"                    Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="3"                           x:Name="tContacttitle" Style="{Staticresource tx}"

                                Text="{Binding Path=TheRestaurant.Contacttitle,  TargetNullValue=Doctor}" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="4"

                    Text="Fax:" Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="4"                           x:Name="tFax" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.Fax, Mode=TwoWay}" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="5"                             Text="ID:" Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="5"                           x:Name="tID" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.ID, Mode=TwoWay}"                                     

                                 AcceptsReturn="True" textwrapping="Wrap" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="6"                             Text="Name:" Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="6"                           x:Name="tName" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.Name,  ValidatesOnDataErrors=TruE}" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="7" Text="Phone:"                   Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="7"                           x:Name="tPhone" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.Phone,  ValidatesOnDataErrors=TruE}" IsReadOnly="{Binding IsLocked}"/>

 

                            <TextBlock Grid.column="0" Grid.Row="8"                             Text="Region:"

                        Style="{Staticresource tb}"/>

                            <TextBox Grid.column="1" Grid.Row="8"                           x:Name="tRegion" Style="{Staticresource tx}"

                                 Text="{Binding Path=TheRestaurant.Region, TargetNullValue=Beijing, ValidatesOnDataErrors=TruE}" IsReadOnly="{Binding IsLocked}"/>

                          

                        </Grid>

                    </Border>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

 

因为这个是自定义Dataform我们还可以使用数据绑定功能来让此DataForm作为查看每条记录的容器,只需要绑定RIA service的数据源。

先说如何新增记录吧

需要创建一个子窗体。

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

 

Home.xaml页面添加一个Add Record按钮:

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

Click事件如下:

  NewRestuarant cw = new NewRestuarant();

            CusRestuarantDataForm custDataform = new CusRestuarantDataForm();

            custDataform.Margin = new Thickness(3);

            custDataform.Width = 450;

            custDataform.TheRestaurant = new CustomRstaurant();

            custDataform.IsLocked = false;

 

            cw.LayoutRoot.Children.Add(custDataform);

            cw.HasCloseButton = false;

            cw.title = "New Restuarant Detail";

            cw.Closed += (s,args) =>

                {

                    if (cw.DialogResult.Value && custDataform.IsValid)

                    {

                      

                    }

                };

            cw.Closing += (s,args) =>

                {

                    if (!custDataform.IsValid && cw.DialogResult.value)

                    {

                        messageBox.Show("Some of field values are not valid.");

                        args.Cancel = true;

                    }

                };

            cw.Show();

           运行一下,

    

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

   最后一步是如何使用RIA service更新数据源了。

Closed事件发生后加入如下代码

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

2.       使用SilverLightcontrolToolkit中的皮肤。

这里需要使用到ICommand方法

先创建一个Class

public class ThemeChangeCommand :ICommand

    {

 

        public bool CanExecute(object parameter)

        {

            return true;

        }

 

        public event EventHandler CanExecuteChanged;

 

        public void Execute(object parameter)

        {

            Theme themeContainer = (Theme)((FrameworkElement)((ContentControl)Application.Current.RootVisual).Content).FindName("ThemeContainer");

            String themename = parameter as String;

 

            if (themename == null)

            {

                themeContainer.ThemeUri = null;

            }

            else

            {

                themeContainer.ThemeUri =new  Uri("/System.Windows.Controls.Theming." + themename + ";component/Theme.xaml",UriKind.RelativeOrAbsolutE);

            }

 

            if (CanExecuteChanged != null)

                CanExecuteChanged(this,new EventArgs());

        }

    }

 

第二步我们需要在APP.xaml添加如下一句话:

   <Helper:ThemeChangeCommand x:Key="themeCommand" />

 

最后就是修改@H_474_132@mainPage.xaml中的Contentborder部分。

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

 

运行一下:

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

右键鼠标就能看到不同的皮肤了。选择个黑色的试试:

 

Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme

前提是你安装了最新的Silverlight Toolkit

Reference: http://weblogs.asp.net/fredriknormen/archive/2009/08/10/how-submitchanges-works-in-net-ria-servies.aspx

@H_262_14@

大佬总结

以上是大佬教程为你收集整理的Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme全部内容,希望文章能够帮你解决Silverlight 4+WCF RIA ServiceS商业应用 4---Custom DataForm&ChangeTheme所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:44+wcfcustomdataformriaservicessilverlightwcf商业应用
猜你在找的silverlight相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap