2017-01-05 9 views
0

У меня есть этот класс для кнопок на моем макете, которые связаны с моей малиной Pi 3. На малиновом Pi у меня есть IOT Core Windows 10, а проект Visual Studio - это приложение Universal Blank.Как запустить этот класс в MainPage.cs?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.Devices.Gpio; 

namespace LCDScherm 
{ 
    class ButtonMap 
    { 
     private const int btnLeft = 26; 
     private const int btnUp = 19; 
     private const int btnDown = 13; 
     private const int btnRight = 6; 

     private static GpioController _gpioController; 

     private GpioPin _btnLeft; 
     private GpioPin _btnUp; 
     private GpioPin _btnDown; 
     private GpioPin _btnRight; 

     private string word; 

     private StringBuilder sb = new StringBuilder(); 

     private char[] wordsAndLetters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ',', '.', '!', '?' }; 

     private int i; 

     private ButtonMap() 
     { 
      _gpioController = GpioController.GetDefault(); 
      if (_gpioController == null) { return; } 

      _btnLeft = _gpioController.OpenPin(btnLeft); 
      _btnLeft.SetDriveMode(GpioPinDriveMode.InputPullUp); 
      _btnLeft.DebounceTimeout = TimeSpan.FromMilliseconds(50); 
      _btnLeft.ValueChanged += btnLeftValueChanged; 

      _btnUp = _gpioController.OpenPin(btnUp); 
      _btnUp.SetDriveMode(GpioPinDriveMode.InputPullUp); 
      _btnUp.DebounceTimeout = TimeSpan.FromMilliseconds(50); 
      _btnUp.ValueChanged += btnUpValueChanged; 

      _btnDown = _gpioController.OpenPin(btnDown); 
      _btnDown.SetDriveMode(GpioPinDriveMode.InputPullUp); 
      _btnDown.DebounceTimeout = TimeSpan.FromMilliseconds(50); 
      _btnDown.ValueChanged += btnDownValueChanged; 

      _btnRight = _gpioController.OpenPin(btnRight); 
      _btnRight.SetDriveMode(GpioPinDriveMode.InputPullUp); 
      _btnRight.DebounceTimeout = TimeSpan.FromMilliseconds(50); 
      _btnRight.ValueChanged += btnRightValueChanged; 
     } 

     private void btnLeftValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) 
     { 
      if (e.Edge == GpioPinEdge.FallingEdge) 
      { 
       //go to previous letter 
       int lengthword = sb.Length; 
       sb.Remove(lengthword - 1, 1); 
       word = sb.ToString(); 
       //tbOutput.Clear(); 
       //tbOutput.Text = word; 
      } 
     } 

     private void btnUpValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) 
     { 
      if (e.Edge == GpioPinEdge.FallingEdge) 
      { 
       //We don't want the i to go above the array 
       if (i <= 39) 
       { 
        i = i + 1; 
       } 
       //output on lcd 
       //Output.Text = word + Convert.ToString(wordsAndLetters[i]); 
      } 
     } 

     private void btnDownValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) 
     { 
      if (e.Edge == GpioPinEdge.FallingEdge) 
      { 
       //We don't want the i to go below the array 
       if (i > 0) 
       { 
        i = i - 1; 
       } 
       //output on lcd 
       //tbOutput.Text = word + Convert.ToString(wordsAndLetters[i]); 
      } 
     } 

     private void btnRightValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) 
     { 
      if (e.Edge == GpioPinEdge.FallingEdge) 
      { 
       //go to next letter & if held for a certain amount of time sent the message 
       //tbOutput.Text = word; 
       sb.Append(wordsAndLetters[i]); 
       word = sb.ToString(); 
       i = 0; 
       //tbOutput.Clear(); 
       //tbOutput.Text = word; 
      } 
     } 
    } 
} 

И я хочу ButtonMap быть инициализированы в моей MainPage, которая в настоящее время выглядит как:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace LCDScherm 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 
     LCD lcd; 

     public MainPage() 
     { 
      this.InitializeComponent(); 

      Init(); 
      Run(); 
     } 

     private async void Init() 
     { 
      lcd = new LCD(16, 2); 
      await lcd.InitAsync(7, 8, 25, 24, 23, 18); 
      //await lcd.clearAsync(); 

     } 

     public async void Run() 
     { 
      //await lcd.clearAsync(); 
      lcd.setCursor(0, 0); 
      lcd.write("test"); 
      lcd.setCursor(0, 1); 
      lcd.write("test"); 
     } 

    } 
} 

Как я могу также инициализировать ButtonMap как я сделал с ЖК-класса, потому что, когда Я стараюсь

ButtonMap btnMap = new ButtonMap(); 

Я получаю сообщение об ошибке, недоступной для класса из-за его уровня защиты. Какое быстрое решение для этого?

ответ

3

Вы должны изменить объявление вашего ButtonMap класса public class ButtonMap { ... } вместо того, чтобы просто class ButtonMap { ... } и вам нужно сделать конструктор класса public вместо private (т.е. public ButtonMap() { ... }). Затем вы сможете создать экземпляр своего класса.