Skip to content

Instantly share code, notes, and snippets.

@FreakyAli
Last active August 23, 2022 20:48
Show Gist options
  • Save FreakyAli/0f525107d1be834f33196c806bada7a0 to your computer and use it in GitHub Desktop.
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
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