设为首页 收藏本站
查看: 1111|回复: 0

win8 app MVVM模式+Popup控件的使用

[复制链接]

尚未签到

发表于 2015-5-21 11:41:35 | 显示全部楼层 |阅读模式
  一、metro项目中使用MVVM 模式的实例
  以及使用Popup 来模拟实现Modal Dialogs
  
  1.DelegateCommand类 继承ICommand接口

DSC0000.gif DSC0001.gif View Code

namespace System.Windows.Input
{
    // 摘要:
    //     定义一个命令
    public interface ICommand
    {
        // 摘要:
        //     当出现影响是否应执行该命令的更改时发生。
        event EventHandler CanExecuteChanged;
        // 摘要:
        //     定义用于确定此命令是否可以在其当前状态下执行的方法。
        //
        // 参数:
        //   parameter:
        //     此命令使用的数据。 如果此命令不需要传递数据,则该对象可以设置为 null。
        //
        // 返回结果:
        //     如果可以执行此命令,则为 true;否则为 false。
        bool CanExecute(object parameter);
        //
        // 摘要:
        //     定义在调用此命令时调用的方法。
        //
        // 参数:
        //   parameter:
        //     此命令使用的数据。 如果此命令不需要传递数据,则该对象可以设置为 null。
        void Execute(object parameter);
    }
}  


internal class DelegateCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func _canExecute;
        ///
        /// Initializes a new instance of the DelegateCommand class that
        /// can always execute.
        ///
        /// The execution logic.
        /// If the execute argument is null.
        public DelegateCommand(Action execute)
            : this(execute, null)
        {
        }
        ///
        /// Initializes a new instance of the DelegateCommand class.
        ///
        /// The execution logic.
        /// The execution status logic.
        /// If the execute argument is null.
        public DelegateCommand(Action execute, Func canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            _execute = execute;
            _canExecute = canExecute;
        }
        ///
        /// Occurs when changes occur that affect whether the command should execute.
        ///
        public event EventHandler CanExecuteChanged;
        ///
        /// Raises the  event.
        ///
        [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
            Justification = "This cannot be an event")]
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
        ///
        /// Defines the method that determines whether the command can execute in its current state.
        ///
        /// This parameter will always be ignored.
        /// true if this command can be executed; otherwise, false.
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }
        ///
        /// Defines the method to be called when the command is invoked.
        ///
        /// This parameter will always be ignored.
        public void Execute(object parameter)
        {
            if (CanExecute(parameter))
            {
                _execute();
            }
        }
    }  
  Models部分:
  PinkFerrari 类继承BindableBase 类


public class PinkFerrari : BindableBase
    {
        private string name;
        private bool isSelected;
        public string Name
        {
            get { return name; }
            set { this.SetProperty(ref name, value); }
        }
        public string ImagePath { get; set; }
        public ImageSource Image
        {
            get
            {
                return new BitmapImage(new Uri("ms-appx:///" + this.ImagePath));
            }
        }
        public bool IsSelected
        {
            get { return isSelected; }
            set { this.SetProperty(ref isSelected, value); }
        }
    }  
  ViewModels 部分:
  CatalogViewModel 类和 MainPageViewModel 类


  public class CatalogViewModel : BindableBase
    {
        private ObservableCollection cars = new ObservableCollection()
            {
                new PinkFerrari{Name = "Enzo", ImagePath=@"Assets/Images/Enzo.jpg"},
                new PinkFerrari{Name = "458 Italia", ImagePath=@"Assets/Images/Italia.jpg"},
                new PinkFerrari{Name = "California", ImagePath=@"Assets/Images/California.jpg"},
                new PinkFerrari{Name = "430", ImagePath=@"Assets/Images/430.jpg"},
                new PinkFerrari{Name = "360 Sbarro", ImagePath=@"Assets/Images/Sbarro.jpg"},
                new PinkFerrari{Name = "Scuderia Spider", ImagePath=@"Assets/Images/Spider.jpg"},
                new PinkFerrari{Name = "599 GTO", ImagePath=@"Assets/Images/599.jpg"}
            };
        public ObservableCollection Cars
        {
            get { return cars; }
        }
    }  


public class MainPageViewModel : BindableBase
    {
        private ICommand openDialogCommand;
        private Popup catalogPopup;
        private CatalogViewModel catalogViewModel;
        private ObservableCollection shoppingList = new ObservableCollection();
        public MainPageViewModel()
        {
            this.openDialogCommand = new DelegateCommand(this.OpenDialog_Executed);
        }
        public ICommand OpenDialogCommand
        {
            get { return this.openDialogCommand; }
        }
        public ObservableCollection ShoppingList
        {
            get { return this.shoppingList; }
        }
        private void OpenDialog_Executed()
        {
            catalogViewModel = new CatalogViewModel();
            CatalogDialog dialog = new CatalogDialog();
            dialog.DataContext = catalogViewModel;
            dialog.CloseRequested += Dialog_CloseRequested;
            this.catalogPopup = new Popup();
            this.catalogPopup.Child = dialog;
            this.catalogPopup.IsOpen = true;
        }
        private void Dialog_CloseRequested(object sender, EventArgs e)
        {
            this.catalogPopup.IsOpen = false;
         
            this.shoppingList.Clear();
            var query = catalogViewModel.Cars.Where(p => p.IsSelected);
            foreach (var item in query)
            {
                this.shoppingList.Add(item);
            }
        }
    }  
  Views 部分:
  CatalogDialog.xaml  、CatalogDialog.xaml.cs
  和MainPage.xaml 、MainPage.xaml.cs



   
        
            
               
                    
                    
                    
               
               
               
                    
                        
                           
                        
                    
                    
                        
                           
                                
                                
                           
                        
                    
               
               
                    
                        
                        
                    
               
            
        
   
  


public sealed partial class CatalogDialog : Page
    {
        public event EventHandler CloseRequested;
        public CatalogDialog()
        {
            this.InitializeComponent();
            var bounds = Window.Current.Bounds;
            this.RootPanel.Width = bounds.Width;
            this.RootPanel.Height = bounds.Height;
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }
        private void CatalogGridView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in e.AddedItems)
            {
                PinkFerrari pf = item as PinkFerrari;
                pf.IsSelected = true;
            }
            foreach (var item in e.RemovedItems)
            {
                PinkFerrari pf = item as PinkFerrari;
                pf.IsSelected = false;
            }
        }
    }  



   
        
   
   
        
            
            
            
        
        
        
            
               
               
            
            
               
                    
                    
               
            
            
               
                    
                        
                    
               
               
                    
                        
                           
                           
                        
                    
               
            
        
        
   
  
DSC0002.png
  
DSC0003.png
  
DSC0004.png
  
DSC0005.jpg
  
  文章来源:http://blogs.u2u.be/diederik

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69202-1-1.html 上篇帖子: [置顶] Win8.1慎用360优化,可能导致安装驱动出现数据无效的问题。附解决方法 下篇帖子: 联想笔记本V470安装Win8.1 X64位系统,关机黑屏、电源灯亮
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表