Мой код выглядит примерно так, сокращенно до минимально используемого:
XAML:
Код: Выделить всё
Код: Выделить всё
public partial class SecondPage: ContentPage
{
private DisplayModel _list;
...
public SecondPage()
{
InitializeComponent();
_list = new DisplayModel(this);
...
}
public async Task DisplayAlertMessage(string name)
{
var confirmation = await DisplayAlert(
"Remove Confirmation",
$"Are you sure you want to remove {name} ?",
"Yes",
"No");
return confirmation;
}
...
--Build a list to use for the grid using individual items
public class DisplayModel : BindableObject
{
public ObservableCollection itemList { get; set; } = new();
private readonly SecondPage _reference;
...
public DisplayModel(SecondPage reference)
{
..
_reference = reference;
GetList();
}
public void GetList()
{
itemList.Clear();
var directoryList = Directory.GetFiles(FolderPath).Select(Path.GetFileName).ToArray();
foreach (string? file in directoryList)
{
itemList.Add(new LoadedItem
{
ref = _reference,
FileName = file,
DirectoryPath = Path
});
}
}
}
--Actual Individual items for use in the grid
public partial class LoadedItem
{
public SecondPage? ref { get; set; }
public string? FileName { get; set; }
public string? DirectoryPath { get; set; }
public ICommand RemoveFile => new Command(RemoveFileCommand);
public void RemoveFileCommand()
{
if (FileName != null && DirectoryPath != null)
{
bool confirmation = ref.DisplayAlertMessage(FileName).Result;
if (confirmation)
{
File.Delete(Path.Combine(DirectoryPath, FileName));
ref._list.GetList();
}
}
}
}
}
Однако DisplayAlertMessage работает, если я вызываю его с уровня SecondPage, на котором оно создано, а не с уровней DisplayModel или LoadedItem.
Что я пропустил?
Подробнее здесь: https://stackoverflow.com/questions/798 ... to-use-a-d
Мобильная версия