如何在Xamarin表单按钮上访问我的ViewModel命令



我已经为我的Xamarin表单项目写了一些命令为我的界面编写了一些命令。我需要在我的按钮上访问这些命令,以便它们执行任务,但是我不确定如何做。

我的ViewModel

thedemo/viewModel/menupageViewModel.cs

using System;
using System.Windows.Input;
using Xamarin.Forms;
namespace TheDemo
{
    public class MenuPageViewModel
    {
        public ICommand GoDashboardCommand { get; set; }
        public ICommand GoRequirementsCommand { get; set; }
        public ICommand GoFixturesCommand { get; set; }
        public ICommand GoVesselsCommand { get; set; }
        public ICommand GoDutyBrokerCommand { get; set; }
        public ICommand GoSettingsCommand { get; set; }
        public ICommand GoInfoCommand { get; set; }
        public MenuPageViewModel()
        {
            GoDashboardCommand = new Command(GoDashboard);
            GoRequirementsCommand = new Command(GoRequirements);
            GoFixturesCommand = new Command(GoFixtures);
            GoVesselsCommand = new Command(GoVessels);
            GoDutyBrokerCommand = new Command(GoBroker);
            GoSettingsCommand = new Command(GoSettings);
            GoInfoCommand = new Command(GoInfo);
        }
        void GoDashboard(object obj) {
            App.NavigationPage.Navigation.PopToRootAsync();
            App.MenuIsPresented = false;
        }
        void GoRequirements(object obj) {
            App.NavigationPage.Navigation.PushAsync(new Requirements());
            App.MenuIsPresented = false;
        }
        void GoFixtures(object obj) {
            App.NavigationPage.Navigation.PushAsync(new Fixtures());
            App.MenuIsPresented = false;
        }
        void GoVessels(object obj) {
            App.NavigationPage.Navigation.PushAsync(new Vessels());
            App.MenuIsPresented = false;
        }
        void GoBroker(object obj) {
            App.NavigationPage.Navigation.PushAsync(new Duty());
            App.MenuIsPresented = false;
        }
        void GoSettings(object obj) {
            App.NavigationPage.Navigation.PushAsync(new Settings());
            App.MenuIsPresented = false;
        }
        void GoInfo(object obj) {
            App.NavigationPage.Navigation.PushAsync(new Information());
            App.MenuIsPresented = false;
        }
    }
}

菜单thedemo/menupage.cs

using System;
using System.Collections.Generic;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.Xamarin.Forms;
using System.Windows.Input;
using Xamarin.Forms;
namespace TheDemo
{
    public partial class MenuPage : ContentPage
    {
        public MenuPage()
        {
            InitializeComponent();
            BindingContext = new MenuPageViewModel();
            Grid grid = new Grid();
            grid.Children.Add(
                        new Button {
                            Text = "Requirements",
                            BackgroundColor = Color.Orange,
                            TextColor = Color.White,
                            Command = //How do I access my commands in my viewmmodel?
                        }, 0, 1);
        }
    }
}

您应该将Button设置为变量,然后通过SetBinding绑定。

示例代码:

var ButtonHolder = new Button {
                        Text = "Requirements",
                        BackgroundColor = Color.Orange,
                        TextColor = Color.White
                    };
ButtonHolder.SetBinding (Button.CommandProperty, "GoDashboardCommand");

grid.Children.Add(ButtonHolder, 0, 1);

首先,将视图模型映射到视图。为此,请在XAML中添加以下行中的视图标签。

添加

的行
xmlns:viewModels="clr-namespace:TheDemo.ViewModels; assembly=TheDemo"

示例

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:viewModels="clr-namespace:TheDemo.ViewModels; assembly=TheDemo">

然后在内容页面中添加此片段

<ContentPage.BindingContext>
        <viewModels:MenuPageViewModel/>
    </ContentPage.BindingContext>

然后添加按钮并使用命令属性,例如此

<Button Command="{Binding GoInfoCommand}"/>

根据您的应用程序更新组件和名称空间。希望这能解决您的问题。

最新更新