2017-02-20 72 views
0

我已經爲我的Xamarin Forms項目的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

你應該設置你的Button在一個變量然後通過將其綁定。

示例代碼:

所有的
var ButtonHolder = new Button { 
         Text = "Requirements", 
         BackgroundColor = Color.Orange, 
         TextColor = Color.White 
        }; 

ButtonHolder.SetBinding (Button.CommandProperty, "GoDashboardCommand"); 


grid.Children.Add(ButtonHolder, 0, 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}"/> 

根據您的應用程序更新程序集和名稱空間。希望這解決了你的問題。

相關問題