zoukankan      html  css  js  c++  java
  • 简易的WPF MVVM模式开发

    • Model层
     public class Song
        {
            private string _artistName;
            private string _songTitle;
    
            public string SongTitle
            {
                get { return _songTitle; }
                set { _songTitle = value; }
            }
    
    
            public string ArtistName
            {
                get { return _artistName; }
                set { _artistName = value; }
            }
    
        }
    • ViewModel

    RelayCommand

     public class RelayCommand : ICommand
        {
    
            private readonly Func<Boolean> _canExecute;
    
            private readonly Action<object> _execute;
    
            public RelayCommand(Action<object> execute)
                : this(execute, null)
            {
    
            }
    
            public RelayCommand(Action<object> execute, Func<bool> canExecute)
            {
                if (execute == null)
                    throw new ArgumentNullException("execute is null");
                _canExecute = canExecute;
                _execute = execute;
            }
    
            public bool CanExecute(object parameter)
            {
                return _canExecute == null ? true : _canExecute();
            }
    
            public event EventHandler CanExecuteChanged
            {
                add
                {
                    if (_canExecute != null)
                    {
                        CommandManager.RequerySuggested += value;
                    }
                }
                remove
                {
                    if (_canExecute != null)
                    {
                        CommandManager.RequerySuggested -= value;
                    }
                }
            }
    
            public void Execute(object parameter)
            {
                _execute(parameter);
            }
        }
     public sealed class SongVM : INotifyPropertyChanged
        {
            private Song _song;
    
            public SongVM()
            {
                _song = new Song() { SongTitle = "叮叮当", ArtistName = "wjp" };
            }
    
            public Song Song
            {
                get { return _song; }
                set
                {
                    _song = value;
                }
            }
    
            public string ArtistName
            {
                get { return Song.ArtistName; }
                set
                {
                    if (ArtistName != value)
                    {
                        Song.ArtistName = value;
                        RaisePropertyChanged("ArtistName");
                    }
                }
            }
    
            public string SongTitle
            {
                get { return Song.SongTitle; }
                set
                {
                    if (SongTitle!=value)
                    {
                        Song.SongTitle = value;
                        RaisePropertyChanged("SongTitle");
                    }
                }
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            private void Execute(object para)
            {
                if (para != null)
                    ArtistName = para.ToString();
            }
    
            private bool CanExeCute()
            {
                return true;
            }
    
            public ICommand UpdateAtistName
            {
                get
                {
                    return new RelayCommand(Execute, CanExeCute);
                }
            }
        }
    • View
    <Window x:Class="MVVMLightDemo.View.SampleMVVM"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:localVM="clr-namespace:MVVMLightDemo.ViewModel"
            Title="SampleMVVM" Height="200" Width="200">
        <Window.DataContext>
            <localVM:SongVM/>
        </Window.DataContext>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="姓名" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="歌曲名称" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding ArtistName}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Song.SongTitle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Button Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Margin="5" Content="更新姓名" Command="{Binding UpdateAtistName}" CommandParameter="王俊鹏"/>
        </Grid>
    </Window>
  • 相关阅读:
    SSH使用TCP Wrappers实现访问控制
    Java设计模式(15)——行为模式之策略模式(Strategy)
    Java设计模式(14)——行为模式之不变模式(Immutable)
    Java设计模式(13)——结构型模式之桥梁模式(Bridge)
    Java设计模式(12)——结构型模式之门面模式(Facade)
    Java设计模式(11)——结构型模式之享元模式(Flyweight)
    Java设计模式(10)——结构型模式之代理模式(Proxy)
    Java设计模式(9)——结构型模式之装饰模式(Decorator)
    Java设计模式(8)——结构型模式之组合模式(Composite)
    Java设计模式(7)——结构型模式之适配器模式(Adapter)
  • 原文地址:https://www.cnblogs.com/smartsensor/p/5211794.html
Copyright © 2011-2022 走看看