Это MainPage.xaml
Код: Выделить всё
Код: Выделить всё
using Contract;
using Microsoft.Maui.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Text;
namespace PrimeNumApp
{
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
private bool _isImplemented;
private Assembly _assembly;
private string _realizationPath;
private IPrimeGenerator _primeNumberGenerator;
private const int PageSize = 5;
private int _currentPage = 0;
public ObservableCollection DisplayablePrimeNums { get; set; }
private List _allPrimeNumbers = new List();
public MainPage()
{
InitializeComponent();
DisplayablePrimeNums = new ObservableCollection();
PrimeNumCollection.ItemsSource = DisplayablePrimeNums;
PrimeNumCollection.RemainingItemsThresholdReached += OnCollectionViewRemainingItemsThresholdReached;
}
public async void OnStartClicked(object sender, EventArgs e)
{
if (int.TryParse(NumberEntry.Text, out int number))
{
try
{
_allPrimeNumbers = await Task.Run(() => _primeNumberGenerator!.GetPrimeNumbers(number));
DisplayablePrimeNums.Clear();
LoadNextPage();
}
catch (Exception ex)
{
await DisplayAlert("Ошибка", ex.Message, "OK");
}
}
else
{
await DisplayAlert("Ошибка", "Вы ввели не натуральное число", "OK");
}
}
private void LoadNextPage()
{
foreach (int number in _allPrimeNumbers.GetRange(_currentPage, PageSize))
{
DisplayablePrimeNums.Add(number);
}
_currentPage += PageSize;
}
public void OnCollectionViewRemainingItemsThresholdReached(object sender, EventArgs e)
{
LoadNextPage();
}
public async void OnLoadClicked(object sender, EventArgs e)
{
await PickDll();
}
private async Task PickDll()
{
var customFileType = new FilePickerFileType(
new Dictionary
{
{ DevicePlatform.WinUI, new[] { ".dll" } },
});
try
{
var result = await FilePicker.PickAsync(new PickOptions()
{
FileTypes = customFileType,
PickerTitle = "Выберите файл формата .dll"
});
if (result != null)
{
_realizationPath = result.FullPath;
await LoadDll();
}
}
catch (Exception ex)
{
await DisplayAlert("Ошибка", "Ошибка при загрузке сборки: " + ex.Message, "OK");
}
}
private async Task LoadDll()
{
_assembly = Assembly.LoadFrom(_realizationPath);
await CheckContractRealization();
}
private async Task CheckContractRealization()
{
_isImplemented = _assembly.GetTypes().Any(type => type.GetInterfaces().Contains(typeof(IPrimeGenerator)));
if (_isImplemented)
{
LoadBtn.BackgroundColor = Colors.Green;
StartBtn.IsEnabled = true;
StartBtn.BackgroundColor = Colors.Gray;
var primeGeneratorType = _assembly.GetTypes()
.Where(type => typeof(IPrimeGenerator).IsAssignableFrom(type) && type.IsClass)
.Select(type => type)
.First();
_primeNumberGenerator = (IPrimeGenerator)Activator.CreateInstance(primeGeneratorType)!;
}
else
{
await DisplayAlert("Ошибка", "Реализация не соответсвует контракту", "OK");
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... t-maui-app