Last active
August 23, 2022 20:48
-
-
Save FreakyAli/0f525107d1be834f33196c806bada7a0 to your computer and use it in GitHub Desktop.
An example of how you can use a basic ViewModel setup to demonstrate data transfer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Windows.Input; | |
using NDEF.MAUI; | |
using NDEF.MAUI.Interfaces; | |
public class MainViewModel : BaseViewModel | |
{ | |
private readonly INfcService nfcAdapter; | |
private string stringData; | |
public string StringData | |
{ | |
get => stringData; | |
set => SetProperty(ref stringData, value); | |
} | |
public ICommand StartNfcTransmissionCommand { get; set; } | |
public MainViewModel(INfcService nfcService) | |
{ | |
this.nfcAdapter = nfcService; | |
StartNfcTransmissionCommand = new Command(() => ExecuteNfc()); | |
stringData = string.Empty; | |
} | |
public async override void ExecuteOnAppearing() | |
{ | |
base.ExecuteOnAppearing(); | |
if (await nfcAdapter.OpenNFCSettingsAsync()) | |
{ | |
nfcAdapter.ConfigureNfcAdapter(); | |
nfcAdapter.EnableForegroundDispatch(); | |
} | |
} | |
public override void ExecuteOnDisappearing() | |
{ | |
base.ExecuteOnDisappearing(); | |
nfcAdapter.DisableForegroundDispatch(); | |
nfcAdapter.UnconfigureNfcAdapter(); | |
} | |
private Task ExecuteNfc() | |
{ | |
byte[] bytes = Encoding.ASCII.GetBytes(stringData); | |
return nfcAdapter.SendAsync(bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment