我在我的viewmodel中为我的Xamarin Forms项目写了一些命令。我需要在我的按钮上访问这些命令,以便他们执行任务但是我不知道该怎么做。
我的ViewModel
TheDemo /视图模型/ 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);
}
}
}
答案 0 :(得分:0)
您应该在变量中设置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);
答案 1 :(得分:0)
首先,将视图模型映射到视图。为此,请将以下行添加到XAML中的视图的ContentPage标记中,如下所示。
要添加的行
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}"/>
根据您的应用程序更新程序集和命名空间。希望这能解决你的问题。