From: zhouleonlei <56956725+zhouleonlei@users.noreply.github.com> Date: Wed, 10 Jun 2020 11:57:06 +0000 (+0800) Subject: [NUI] Add command for button (#1647) X-Git-Tag: accepted/tizen/unified/20210219.040944~687 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e5a04605638786914c87876fa5ad291032ab0991;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [NUI] Add command for button (#1647) * [NUI] Add command for button * [NUI] Add Command XAML sample --- diff --git a/src/Tizen.NUI.Components/Controls/Button.cs b/src/Tizen.NUI.Components/Controls/Button.cs index 3d8d160..13526bb 100755 --- a/src/Tizen.NUI.Components/Controls/Button.cs +++ b/src/Tizen.NUI.Components/Controls/Button.cs @@ -1199,6 +1199,7 @@ namespace Tizen.NUI.Components private void OnClickInternal(ClickEventArgs eventArgs) { + Command?.Execute(CommandParameter); OnClick(eventArgs); Extension?.OnClick(this, eventArgs); ClickEvent?.Invoke(this, eventArgs); diff --git a/src/Tizen.NUI.Components/Controls/Control.cs b/src/Tizen.NUI.Components/Controls/Control.cs index 0a46c99..4e62577 100755 --- a/src/Tizen.NUI.Components/Controls/Control.cs +++ b/src/Tizen.NUI.Components/Controls/Control.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.ComponentModel; using Tizen.NUI.BaseComponents; using Tizen.NUI.Binding; +using System.Windows.Input; namespace Tizen.NUI.Components { @@ -30,6 +31,15 @@ namespace Tizen.NUI.Components [EditorBrowsable(EditorBrowsableState.Never)] public class Control : VisualView { + /// Internal used. + [EditorBrowsable(EditorBrowsableState.Never)] + public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(Control), null, propertyChanged: (bo, o, n) => ((Control)bo).OnCommandChanged()); + + /// Internal used. + [EditorBrowsable(EditorBrowsableState.Never)] + public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(Button), null, + propertyChanged: (bindable, oldvalue, newvalue) => ((Button)bindable).CommandCanExecuteChanged(bindable, EventArgs.Empty)); + /// Control style. /// 6 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API. @@ -104,9 +114,20 @@ namespace Tizen.NUI.Components Initialize(style); } - internal void ApplyAttributes(View view, ViewStyle viewStyle) + /// Internal used. + [EditorBrowsable(EditorBrowsableState.Never)] + public ICommand Command + { + get { return (ICommand)GetValue(CommandProperty); } + set { SetValue(CommandProperty, value); } + } + + /// Internal used. + [EditorBrowsable(EditorBrowsableState.Never)] + public object CommandParameter { - view.CopyFrom(viewStyle); + get { return GetValue(CommandParameterProperty); } + set { SetValue(CommandParameterProperty, value); } } /// @@ -117,6 +138,22 @@ namespace Tizen.NUI.Components internal bool IsFocused { get; set; } = false; + internal void CommandCanExecuteChanged(object sender, EventArgs eventArgs) + { + ICommand cmd = Command; + if (cmd != null) + cmd.CanExecute(CommandParameter); + } + + internal void OnCommandChanged() + { + if (Command != null) + { + Command.CanExecuteChanged += CommandCanExecuteChanged; + CommandCanExecuteChanged(this, EventArgs.Empty); + } + } + /// /// Dispose Control and all children on it. /// diff --git a/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml new file mode 100755 index 0000000..a91e7e8 --- /dev/null +++ b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + diff --git a/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml.cs b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml.cs new file mode 100755 index 0000000..0a8348b --- /dev/null +++ b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml.cs @@ -0,0 +1,162 @@ +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Components; + +namespace Tizen.NUI.Examples +{ + public class NameModel + { + string userName; + string companyName; + + public string UserName + { + get + { + return userName; + } + set + { + userName = value; + } + } + + public string CompanyName + { + get + { + return companyName; + } + set + { + companyName = value; + } + } + } + + public class NameViewModel : INotifyPropertyChanged + { + public NameViewModel() + { + userName = new NameModel() + { + UserName = "Adun", + CompanyName = "Samsung" + }; + } + + NameModel userName; + + public event PropertyChangedEventHandler PropertyChanged; + + public string UserName + { + get + { + return userName.UserName; + } + set + { + userName.UserName = value; + RaisePropertyChanged("UserName"); + } + } + + public string CompanyName + { + get + { + return userName.CompanyName; + } + set + { + userName.CompanyName = value; + } + } + + private void RaisePropertyChanged(string propertyName) + { + PropertyChangedEventHandler handler = PropertyChanged; + if (null != handler) + { + handler(this, new PropertyChangedEventArgs(propertyName)); + } + } + + void UpdateNameExecute() + { + UserName = "Xiaohui Fang"; + } + + bool CanUpdateNameExecute() + { + return true; + } + + public ICommand UpdateName { get { return new RelayCommand(UpdateNameExecute, CanUpdateNameExecute); } } + } + + public class RelayCommand : ICommand + { + readonly Func _canExecute; + readonly Action _execute; + + public RelayCommand(Action execute) + : this(execute, null) + { + } + + public RelayCommand(Action execute, Func canExecute) + { + if (execute == null) + throw new ArgumentNullException("execute"); + + _execute = execute; + _canExecute = canExecute; + } + + public event EventHandler CanExecuteChanged + { + add + { + if (_canExecute != null) + { + //CommandManager.RequerySuggested += value; + } + } + remove + { + if (_canExecute != null) + { + //CommandManager.RequerySuggested -= value; + } + + } + } + + [DebuggerStepThrough] + public bool CanExecute(Object parameter) + { + return _canExecute == null ? true : _canExecute(); + } + + public void Execute(Object parameter) + { + _execute(); + } + } + + public partial class CommandDemo + { + public CommandDemo() + { + InitializeComponent(); + } + } +} diff --git a/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemoApplication.cs b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemoApplication.cs new file mode 100755 index 0000000..d794952 --- /dev/null +++ b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemoApplication.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tizen.NUI.Components; + +namespace Tizen.NUI.Examples +{ + public class CommandDemoApplication : NUIApplication + { + protected override void OnCreate() + { + CommandDemo demo = new CommandDemo(); + Window.Instance.Add(demo); + } + } +} diff --git a/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.cs b/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.cs index 5cd226f..3cff637 100755 --- a/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.cs +++ b/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.cs @@ -18,6 +18,7 @@ namespace NUIXAMLTestSample // new TestAmbient().Run(args); // new TestDetailApps().Run(args); // new TestMyContents().Run(args); + // new CommandDemoApplication().Run(args); /* For UTC codes */ // new TestButton().Run(args); diff --git a/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.csproj b/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.csproj index c911c47..7161c1e 100755 --- a/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.csproj +++ b/test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.csproj @@ -13,7 +13,7 @@ - + @@ -47,6 +47,7 @@ + @@ -72,7 +73,7 @@ + -