2017. 4. 18. 10:38

Command Pattern



선언

    public class DelegateCommand : ICommand
    {
        private Action<object> _execute;
        private Predicate<object> _canExecute;

        public DelegateCommand(Action<object> execute)
               : this(execute, null)
        {
        }

        public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged = delegate { };

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }


구현

        private ICommand _addCommand;
        public ICommand AddCommand
        {
            get
            {
                return _addCommand ??
                    (_addCommand = new DelegateCommand(
                        p => { 
                            // Add 로직 
                        },
                        p => { // Command 사용여부 
                                  return true; 
                        }
                    ));
            }
        }




Xaml

<Button Content="Add" Command={Binding AddCommand} />

<Window.InputBidings>
    <KeyBinding Key="A"
                       Modifiers="Control"
                       Command={Binding AddCommand} />
</Window.InputBidings>




Posted by CoolDragon
2017. 4. 17. 16:39

WPF

if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))

{

  // Design Time Code

}

else

{

  // Runtime Code

}


WindowPhone, Silverlight

if (DesignerProperties.IsInDesignTool)

{

  // Design Time Code

}

else

{

  // Runtime Code

}


Windows Store

if (DesignerProperties.DesignModeEnabled)

{

  // Design Time Code

}

else

{

  // Runtime Code

}






Posted by CoolDragon
2017. 4. 17. 14:39
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        protected virtual void RaisePropertyChangedEvent([CallerMemberName] string propertyName = null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void SetProperty<T>(ref T member, T val, [CallerMemberName] string propertyName = null)
        {
            if (object.Equals(member, val)) return;

            member = val;
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    
    public class ViewModelBase : ObservableObject
    {
        public virtual bool IsInDesignMode()
        {
#if WINDOWS_PHONE_APP
            if (DesignerProperties.IsInDesignTool)
            {
              return true;
            }
            else
            {
              return false;
            }
#else
            if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
            {
                return true;
            }
            else
            {
                return false;
            }
#endif
        }
    }


Posted by CoolDragon
2017. 4. 15. 13:46

각 Device별로 설정값을 저장하는 위치가 다르다.

이 것을 아래의 플러그인이 손쉽게 해당 설정값에 접근하기 쉽게 도와준다.

Settings Plugin for Xamarin and Windows



CrossSettings.Current.AddOrUpdateValue<string>("Key", "Value");

string value = CrossSettings.Current.GetValueOrDefault<string>("Key", "Unknown");


Posted by CoolDragon
2017. 4. 15. 13:10

1. install Xamarin

  - Download

  - 인스톨 후 Xamarin Studio를 실행 > 메뉴에서 Account를 선택 후 계정 생성


2. Install Emulator

  아래 필요한 에뮬레이터를 설치한다.

  - android-player

  - BlueStacks

  - Genymotion

  - Andy

Posted by CoolDragon