[NUI] Add command for button (#1647)
authorzhouleonlei <56956725+zhouleonlei@users.noreply.github.com>
Wed, 10 Jun 2020 11:57:06 +0000 (19:57 +0800)
committerGitHub <noreply@github.com>
Wed, 10 Jun 2020 11:57:06 +0000 (20:57 +0900)
* [NUI] Add command for button

* [NUI] Add Command XAML sample

src/Tizen.NUI.Components/Controls/Button.cs
src/Tizen.NUI.Components/Controls/Control.cs
test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml [new file with mode: 0755]
test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml.cs [new file with mode: 0755]
test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemoApplication.cs [new file with mode: 0755]
test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.cs
test/NUITestSample/NUIXAMLTestSample/NUIXAMLTestSample.csproj

index 3d8d160..13526bb 100755 (executable)
@@ -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);
index 0a46c99..4e62577 100755 (executable)
@@ -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));
+
         /// <summary> Control style. </summary>
         /// <since_tizen> 6 </since_tizen>
         /// 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); }
         }
 
         /// <summary>
@@ -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);
+            }
+        }
+
         /// <summary>
         /// Dispose Control and all children on it.
         /// </summary>
diff --git a/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml
new file mode 100755 (executable)
index 0000000..a91e7e8
--- /dev/null
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<b:View x:Class="Tizen.NUI.Examples.CommandDemo"
+  xmlns="http://tizen.org/Tizen.NUI/2018/XAML"
+  xmlns:b="clr-namespace:Tizen.NUI.BaseComponents;assembly=Tizen.NUI"
+  xmlns:u="clr-namespace:Tizen.NUI.UIComponents;assembly=Tizen.NUI"
+  xmlns:l="clr-namespace:Tizen.NUI.Examples"
+  xmlns:c="clr-namespace:Tizen.NUI.Components;assembly=Tizen.NUI.Components"
+  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
+
+  <View.BindingContext>
+    <l:NameViewModel x:Name="nameViewModel"/>
+  </View.BindingContext>
+
+  <b:TextLabel Text="UserName" Size2D="300,50" Position2D="500,200" BackgroundColor="Yellow" PointSize ="20"/>
+  <b:TextLabel Text="{Binding UserName}" Size2D="300,50" Position2D="500,270" BackgroundColor="Blue" PointSize ="20"/>
+  <b:TextLabel Text="CompanyName" Size2D="300,50" Position2D="500,340" BackgroundColor="Yellow" PointSize ="20"/>
+  <b:TextLabel Text="{Binding CompanyName}" Size2D="300,50" Position2D="500,410" BackgroundColor="Blue" PointSize ="20"/>
+  <c:Button Text="Update" Size2D="200,100" Position2D="500,480" Command="{Binding UpdateName}"/>
+</b:View>
diff --git a/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml.cs b/test/NUITestSample/NUIXAMLTestSample/CommandDemo/CommandDemo.xaml.cs
new file mode 100755 (executable)
index 0000000..0a8348b
--- /dev/null
@@ -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<Boolean> _canExecute;
+        readonly Action _execute;
+
+        public RelayCommand(Action execute)
+            : this(execute, null)
+        {
+        }
+
+        public RelayCommand(Action execute, Func<Boolean> 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 (executable)
index 0000000..d794952
--- /dev/null
@@ -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);
+        }
+    }
+}
index 5cd226f..3cff637 100755 (executable)
@@ -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);
index c911c47..7161c1e 100755 (executable)
@@ -13,7 +13,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Tizen.NET" Version="8.0.0.15214" />
+    <PackageReference Include="Tizen.NET" Version="8.0.0.15258" />
     <PackageReference Include="Tizen.NET.Sdk" Version="1.0.0" />
     <PackageReference Include="Tizen.NUI.XamlBuild" Version="1.0.11" />
   </ItemGroup>
@@ -47,6 +47,7 @@
       <None Remove="res\layout\TextFieldTestPage.xaml" />
       <None Remove="res\layout\TextLabelTestPage.xaml" />
       <None Remove="res\layout\VideoViewPage.xaml" />
+      <None Remove="CommandDemo\CommandDemo.xaml" />
     </ItemGroup>
     <ItemGroup>
       <EmbeddedResource Include="res\layout\1920x1080\AmbientDuoPage.xaml" />
@@ -72,7 +73,7 @@
       <EmbeddedResource Include="res\layout\TextFieldTestPage.xaml" />
       <EmbeddedResource Include="res\layout\TextLabelTestPage.xaml" />
       <EmbeddedResource Include="res\layout\VideoViewPage.xaml" />
+      <EmbeddedResource Include="CommandDemo\CommandDemo.xaml" />
     </ItemGroup>
-
 </Project>