简体   繁体   中英

Command Chaining in UWP

ITNOA

I have a UWP custom control that have a method and I want to call this method in some ViewModel (I use CommunityToolkit.Mvvm framework in our project), after many searches I found similar solution in Xamarin land that called Command Chaining and the articles says:

Command chaining is the most MVVM friendly approach as it leaves everything to the binding system to connect up and the View and ViewModel still have no direct knowledge of each other. The only issue is you must extend the control and it adds a bit of extra complexity.

But my problem is, I do not know how to implement Command Chaining in UWP.

I can add DependencyProperty to my custom control like below

    /// My Custom UWP Control
    public partial class StatusBar : UserControl {

        public StatusBar() {
            InitializeComponent();

            RefreshCommand = new RelayCommand(() => { this.RefreshStatus(); });
        }

        public static DependencyProperty RefreshCommandProperty = DependencyProperty.Register("RefreshCommand", typeof(ICommand), typeof(StatusBar), new PropertyMetadata(null));

        public ICommand RefreshCommand
        {
            get { return (ICommand)GetValue(RefreshCommandProperty); }
            set { SetValue(RefreshCommandProperty, value); }
        }

        public void RefreshStatus() {
            RegistrationState state;
            if (LinphoneManager.Instance.Core.DefaultProxyConfig == null)
                state = RegistrationState.None;
            else
                state = LinphoneManager.Instance.Core.DefaultProxyConfig.State;

            RefreshStatus(state);
        }
    }

and View XAML file like below

            <controls:StatusBar 
            x:Name="status" 
            Grid.Row="0"
            RefreshCommand="{Binding RefreshCommand}"
            Tapped="status_Tapped"/>

But I do not know how to add Command in my view model, because in my view model I do not have GetValue and SetValue as you can see in article example.

Note: My source code is here

thanks

Command Chaining in UWP

The is no such Command Chaining behavior in UWP platform. If you want to bind StatusBar RefreshCommand with a other button command, you could access StatusBar RefreshCommand property with ElementName markup.

For example

<local:StatusBar x:Name="MyStatusBar" />
<Button Visibility="Visible" Content="Show" Command="{Binding ElementName=MyStatusBar, Path=RefreshCommand}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM