简体   繁体   English

UWP 中的命令链

[英]Command Chaining in UWP

ITNOA 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:我有一个 UWP 自定义控件,它有一个方法,我想在一些 ViewModel 中调用这个方法(我在我们的项目中使用 CommunityToolkit.Mvvm 框架),经过多次搜索,我在Xamarin 领域找到了类似的解决方案,称为命令链,文章说:

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.命令链是对 MVVM 最友好的方法,因为它将所有内容留给绑定系统来连接,而 View 和 ViewModel 仍然没有直接的相互了解。 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.但我的问题是,我不知道如何在 UWP 中实现命令链。

I can add DependencyProperty to my custom control like below我可以将DependencyProperty添加到我的自定义控件中,如下所示

    /// 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并查看 XAML 文件,如下所示

            <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.但我不知道如何在我的视图模型中添加命令,因为在我的视图模型中我没有GetValueSetValue ,如文章示例中所示。

Note: My source code is here注意:我的源代码在这里

thanks谢谢

Command Chaining in UWP UWP 中的命令链

The is no such Command Chaining behavior in UWP platform. UWP 平台中没有这样的命令链接行为。 If you want to bind StatusBar RefreshCommand with a other button command, you could access StatusBar RefreshCommand property with ElementName markup.如果要将 StatusBar RefreshCommand 与其他按钮命令绑定,可以使用ElementName标记访问 StatusBar RefreshCommand 属性。

For example例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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