日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows Phone 7 MVVM模式数据绑定和传递参数

發(fā)布時(shí)間:2024/4/14 windows 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows Phone 7 MVVM模式数据绑定和传递参数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)綁定使用了ObservableCollection<T> 類來實(shí)現(xiàn),ViewModel通過繼承GalaSoft.MvvmLight.ViewModelBase類來實(shí)現(xiàn),Command

使用GalaSoft.MvvmLight.Command.RelayCommand<T>來實(shí)現(xiàn)。

ObservableCollection<T>表示一個(gè)動態(tài)數(shù)據(jù)集合,在添加項(xiàng)、移除項(xiàng)或刷新整個(gè)列表時(shí),此集合將提供通知。

客戶列表綁定客戶的名字、QQ、地址信息,單擊的時(shí)候顯示客戶的全部詳細(xì)信息。

?

?

View層

?

  • <phone:PhoneApplicationPage?
  • ???xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
  • ???xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
  • ???xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"?
  • ???xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"?
  • ???xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?
  • ???xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?
  • ???xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"?xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"?
  • ???x:Class="MvvmLight5.MainPage"?
  • ???FontFamily="{StaticResource?PhoneFontFamilyNormal}"?
  • ???FontSize="{StaticResource?PhoneFontSizeNormal}"?
  • ???Foreground="{StaticResource?PhoneForegroundBrush}"?
  • ???SupportedOrientations="Portrait"?
  • ???Orientation="Portrait"?
  • ???mc:Ignorable="d"?
  • ???d:DesignWidth="480"?
  • ???d:DesignHeight="768"?
  • ???shell:SystemTray.IsVisible="True"?
  • ???DataContext="{Binding?Main,?Source={StaticResource?Locator}}">?
  • ????<!--綁定ViewModel中的Main實(shí)例對象?資源在App.xaml中進(jìn)行了加載-->?
  • ?
  • ???<Grid?
  • ??????x:Name="LayoutRoot"?
  • ??????Background="Transparent">?
  • ??????<Grid.RowDefinitions>?
  • ?????????<RowDefinition??Height="Auto"?/>?
  • ?????????<RowDefinition?Height="*"?/>?
  • ??????</Grid.RowDefinitions>?
  • ?
  • ??????<StackPanel?x:Name="TitlePanel"??Grid.Row="0"?Margin="24,24,0,12">?
  • ?????????<TextBlock?
  • ????????????x:Name="ApplicationTitle"?
  • ????????????Text="{Binding?ApplicationTitle}"?
  • ????????????Style="{StaticResource?PhoneTextNormalStyle}"?/>?
  • ?????????<TextBlock?
  • ????????????x:Name="PageTitle"?
  • ????????????Text="{Binding?PageName}"?
  • ????????????Margin="-3,-8,0,0"?
  • ????????????Style="{StaticResource?PhoneTextTitle1Style}"?/>?
  • ??????</StackPanel>?
  • ?
  • ??????<Grid??x:Name="ContentGrid"?Grid.Row="1">?
  • ?????????<ListBox??x:Name="PersonListBox"???Margin="10"?ItemsSource="{Binding?Customers}">?
  • ????????????????<!--綁定MainViewModel的Customers數(shù)據(jù)-->?
  • ????????????<ListBox.ItemTemplate>?
  • ???????????????<DataTemplate>?
  • ??????????????????<StackPanel>?
  • ?????????????????????<StackPanel?
  • ????????????????????????x:Name="DataTemplateStackPanel"?
  • ????????????????????????Orientation="Horizontal">?
  • ????????????????????????<TextBlock?
  • ???????????????????????????x:Name="Name"?
  • ???????????????????????????Text="{Binding?Name}"?
  • ???????????????????????????Margin="0,0,5,0"?
  • ???????????????????????????Style="{StaticResource?PhoneTextExtraLargeStyle}"?/>?
  • ????????????????????????<TextBlock?
  • ???????????????????????????x:Name="QQ"?
  • ???????????????????????????Text="{Binding?QQ}"?
  • ???????????????????????????Margin="0"?
  • ???????????????????????????Style="{StaticResource?PhoneTextExtraLargeStyle}"?/>?
  • ?????????????????????</StackPanel>?
  • ?????????????????????<TextBlock?
  • ????????????????????????x:Name="Email"?
  • ????????????????????????Text="{Binding?Address}"?
  • ????????????????????????Margin="0"?
  • ????????????????????????Style="{StaticResource?PhoneTextSubtleStyle}"?/>?
  • ?
  • ??????????????????</StackPanel>?
  • ???????????????</DataTemplate>?
  • ????????????</ListBox.ItemTemplate>?
  • ????????????????<!--添加System.Windows.Interactivity觸發(fā)器處理事件,放在ListBox里面-->?
  • ?????????????<Custom:Interaction.Triggers>?
  • ?????????????????<!--當(dāng)選中客戶的時(shí)候觸發(fā)該事件-->?
  • ????????????????<Custom:EventTrigger?EventName="SelectionChanged">?
  • ????????????????????????<GalaSoft_MvvmLight_Command:EventToCommand?x:Name="SelectionCommand"?Command="{Binding?DetailsPageCommand,?Mode=OneWay}"?CommandParameter="{Binding?SelectedItem,?ElementName=PersonListBox}"/>?
  • ????????????????????????<!--傳遞的參數(shù)為ListBox選中的Customer對象-->?
  • ?????????????????</Custom:EventTrigger>?
  • ?????????????</Custom:Interaction.Triggers>?
  • ?????????</ListBox>?
  • ?
  • ??????</Grid>?
  • ???</Grid>?
  • </phone:PhoneApplicationPage>?
  • ViewModel層

    ViewModelLocator是對ViewModel進(jìn)行初始化和清理的集中處理的類 添加資源的時(shí)候只需要添加這一個(gè)類就可以了。

    ViewModelLocator.cs

    ?

  • namespace?MvvmLight5.ViewModel ?
  • { ?
  • ???public?class?ViewModelLocator ?
  • ???{ ?
  • ??????private?static?MainViewModel?_main; ?
  • ?
  • ??????///?<summary>?
  • ??????///?初始化??在這里創(chuàng)建ViewModel??可以將多個(gè)ViewModel在這里一起創(chuàng)建 ?
  • ??????///?</summary>?
  • ??????public?ViewModelLocator() ?
  • ??????{ ?
  • ?????????CreateMain(); ?
  • ?
  • ??????} ?
  • ?
  • ??????///?<summary>?
  • ??????///?獲取MainViewModel的靜態(tài)的實(shí)例對象 ?
  • ??????///?</summary>?
  • ??????public?static?MainViewModel?MainStatic ?
  • ??????{ ?
  • ??????????get ?
  • ??????????{ ?
  • ??????????????if?(_main?==?null) ?
  • ??????????????{ ?
  • ??????????????????CreateMain(); ?
  • ??????????????} ?
  • ?
  • ??????????????return?_main; ?
  • ??????????} ?
  • ??????} ?
  • ?
  • ??????///?<summary>?
  • ??????///?獲取MainViewModel的實(shí)例對象 ?
  • ??????///?</summary>?
  • ??????public?MainViewModel?Main ?
  • ??????{ ?
  • ??????????get ?
  • ??????????{ ?
  • ??????????????return?MainStatic; ?
  • ??????????} ?
  • ??????} ?
  • ?
  • ??????///?<summary>?
  • ??????///清理MainViewModel?退出程序的時(shí)候進(jìn)行清理??在App.xmal.cs中調(diào)用 ?
  • ??????///?</summary>?
  • ??????public?static?void?ClearMain() ?
  • ??????{ ?
  • ?????????_main.Cleanup(); ?
  • ?????????_main?=?null; ?
  • ??????} ?
  • ?
  • ??????///?<summary>?
  • ??????///?創(chuàng)建MainViewModel ?
  • ??????///?</summary>?
  • ??????public?static?void?CreateMain() ?
  • ??????{ ?
  • ?????????if?(?_main?==?null?) ?
  • ?????????{ ?
  • ????????????_main?=?new?MainViewModel(); ?
  • ?????????} ?
  • ??????} ?
  • ???} ?
  • }?
  • MainViewModel.cs

    ?

  • using?System.Collections.ObjectModel; ?
  • using?GalaSoft.MvvmLight; ?
  • using?GalaSoft.MvvmLight.Command; ?
  • using?MvvmLight5.Model; ?
  • ?
  • namespace?MvvmLight5.ViewModel ?
  • { ?
  • ???public?class?MainViewModel?:?ViewModelBase ?
  • ???{ ?
  • ?
  • ??????///?<summary>?
  • ??????///?數(shù)據(jù)綁定的客戶列表 ?
  • ??????///?</summary>?
  • ??????public?ObservableCollection<Customer>?Customers ?
  • ??????{ ?
  • ?????????get ?
  • ?????????{ ?
  • ????????????var?customerCollection?=?new?CustomerCollection(); ?
  • ????????????return?customerCollection.Customers; ?
  • ?????????} ?
  • ??????} ?
  • ??????//定義Command ?
  • ??????public?RelayCommand<Customer>?DetailsPageCommand ?
  • ??????{ ?
  • ?????????get; ?
  • ?????????private?set; ?
  • ??????} ?
  • ?
  • ??????public?string?ApplicationTitle ?
  • ??????{ ?
  • ?????????get ?
  • ?????????{ ?
  • ????????????return?"MVVM?LIGHT"; ?
  • ?????????} ?
  • ??????} ?
  • ?
  • ??????public?string?PageName ?
  • ??????{ ?
  • ?????????get ?
  • ?????????{ ?
  • ????????????return?"客戶列表如下:"; ?
  • ?????????} ?
  • ??????} ?
  • ?
  • ??????public?string?Welcome ?
  • ??????{ ?
  • ?????????get ?
  • ?????????{ ?
  • ????????????return?"Welcome?to?MVVM?Light"; ?
  • ?????????} ?
  • ??????} ?
  • ?
  • ??????///?<summary>?
  • ??????///?初始化?MainViewModel ?
  • ??????///?</summary>?
  • ??????public?MainViewModel() ?
  • ??????{ ?
  • ??????????//初始化Command ?
  • ?????????DetailsPageCommand?=?new?RelayCommand<Customer>(?(?msg?)?=>?GoToDetailsPage(?msg?)?); ?
  • ??????} ?
  • ?
  • ??????private?object?GoToDetailsPage(?Customer?msg?) ?
  • ??????{ ?
  • ??????????System.Windows.MessageBox.Show("客戶的詳細(xì)資料如下????名字:"?+?msg.Name?+?"??城市:"?+?msg.City?+?"??地址:"?+?msg.Address?+?"??電話:"?+?msg.Phone?+?"??QQ:"?+?msg.QQ); ?
  • ?????????return?null; ?
  • ??????} ?
  • ???} ?
  • }?
  • Model層

    Customers.cs

    ?

  • using?System; ?
  • using?System.Collections.Generic; ?
  • using?System.Collections.ObjectModel; ?
  • ?
  • namespace?MvvmLight5.Model ?
  • { ?
  • ???public?class?CustomerCollection ?
  • ???{ ?
  • ??????//在這里綁定數(shù)據(jù)使用了ObservableCollection<T>?類?? ?
  • ??????private?readonly?ObservableCollection<Customer>?_customers?=?new?ObservableCollection<Customer>(); ?
  • ??????public?ObservableCollection<Customer>?Customers ?
  • ??????{ ?
  • ?????????get?{?return?_customers;?} ?
  • ??????} ?
  • ?
  • ??????public?Customer?GetCustomerByID(?int?id?) ?
  • ??????{ ?
  • ?????????return?_customers[?id?]; ?
  • ??????} ?
  • ?
  • ??????public?CustomerCollection() ?
  • ??????{ ?
  • ?????????try ?
  • ?????????{ ?
  • ????????????GenerateCustomers(); ?
  • ?????????} ?
  • ?????????catch?(?Exception?e?) ?
  • ?????????{ ?
  • ????????????System.Windows.MessageBox.Show(?"Exception:?"?+?e.Message?); ?
  • ?????????} ?
  • ??????} ?
  • ???????//初始化數(shù)據(jù) ?
  • ??????public?void?GenerateCustomers() ?
  • ??????{ ?
  • ????????????_customers.Add(?new?Customer(1,"黃小琥","臺灣高雄市十六街8號","高雄","13457789907","3232","huangxiaohu@qq.com")?); ?
  • ????????????_customers.Add(new?Customer(2,?"李開復(fù)",?"北京市東城區(qū)十六街8號",?"北京",?"136589907",?"787222894",?"huasdsdu@qq.com")); ?
  • ????????????_customers.Add(new?Customer(3,?"周杰倫",?"臺灣臺北市十六街8號",?"臺北",?"145455779907",?"2323266",?"232@qq.com")); ?
  • ????????????_customers.Add(new?Customer(4,?"郎咸平",?"香港十六街8號",?"香港",?"145489907",?"787222894",?"6ggg@qq.com")); ?
  • ????????????_customers.Add(new?Customer(5,?"加菲貓",?"高雄市十六街8號",?"高雄市",?"15777789907",?"333434",?"2323@qq.com")); ?
  • ????????????_customers.Add(new?Customer(6,?"灰太狼",?"臺灣第三代市十六街8號",?"高雄市",?"134357789907",?"23232",?"3232@qq.com")); ?
  • ????????????_customers.Add(new?Customer(7,?"喜洋洋",?"臺灣高雄市十六街8號",?"高雄市",?"134544589907",?"23232777",?"88sds@qq.com")); ?
  • ????????????_customers.Add(new?Customer(8,?"春哥",?"臺灣所得稅十六街8號",?"高雄市",?"13453445907",?"888888",?"sdsgg@qq.com")); ?
  • ????????? ?
  • ??????} ?
  • ???} ?
  • ?
  • ???public?class?Customer ?
  • ???{ ?
  • ??????public?int?ID?{?get;?set;?} ?
  • ?
  • ??????public?string?Name?{?get?;?set;?} ?
  • ?
  • ??????public?string?Address?{?get;?set;?} ?
  • ?
  • ??????public?string?City?{?get;?set;?} ?
  • ?
  • ??????public?string?Phone?{?get;?set;?} ?
  • ?
  • ??????public?string?QQ?{?get;?set;?} ?
  • ?
  • ??????public?string?Email?{?get;?set;?} ?
  • ?
  • ??????public?Customer() ?
  • ??????{?} ?
  • ?
  • ??????public?Customer( ?
  • ?????????int?id, ?
  • ??????????string?name, ?
  • ??????????string?address, ?
  • ??????????string?city, ?
  • ??????????string?phone, ?
  • ??????????string?qq, ?
  • ??????????string?email?) ?
  • ??????{ ?
  • ?????????ID?=?id; ?
  • ?????????Name?=?name; ?
  • ?????????Address?=?address; ?
  • ?????????City?=?city; ?
  • ?????????PhonePhone?=?Phone; ?
  • ?????????QQ?=?qq; ?
  • ?????????Email?=?email; ?
  • ??????} ?
  • ???} ?
  • }?
  • App.xaml 程序初始化處理

    ?

  • <Application?x:Class="MvvmLight5.App"?
  • ?????????????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
  • ?????????????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
  • ?????????????xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"?
  • ?????????????xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"?
  • ?????????????xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?
  • ?????????????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?
  • ?????????????mc:Ignorable="d"?
  • ?????????????xmlns:vm="clr-namespace:MvvmLight5.ViewModel">?
  • ?
  • ????<!--Application?Resources-->?
  • ????<Application.Resources>?
  • ????????<vm:ViewModelLocator?x:Key="Locator"?
  • ?????????????????????????????d:IsDataSource="True"?/>?
  • ????</Application.Resources>?
  • ?
  • ????<Application.ApplicationLifetimeObjects>?
  • ????????<!--Required?object?that?handles?lifetime?events?for?the?application-->?
  • ????????<shell:PhoneApplicationService?Launching="Application_Launching"?
  • ???????????????????????????????????????Closing="Application_Closing"?
  • ???????????????????????????????????????Activated="Application_Activated"?
  • ???????????????????????????????????????Deactivated="Application_Deactivated"?/>?
  • ????</Application.ApplicationLifetimeObjects>?
  • ?
  • </Application>?
  • cs

    ?// 清理ViewModel資源
    ????? private void Application_Closing( object sender, ClosingEventArgs e )
    ????? {
    ???????? ViewModelLocator.ClearMain();
    ????? }
    ?

    轉(zhuǎn)載于:https://blog.51cto.com/linzheng/1078630

    總結(jié)

    以上是生活随笔為你收集整理的Windows Phone 7 MVVM模式数据绑定和传递参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。