XAML 在MAUI社区工具包弹出窗口中使用随机JSON数据选取器

x33g5p2x  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(185)

我创建了随机JSON数据拾取器,我想显示拾取的数据,它在MAUI社区工具包Popop..代码:

private async void OnButton_Clicked1(object sender, EventArgs e)
{
    Random random = new Random();
    int randomdrink = random.Next(1, 5);
    using var stream = await FileSystem.OpenAppPackageFileAsync("drinks.json");
    using var reader = new StreamReader(stream);
    var drinks = JsonSerializer.Deserialize<List<drinksclass>>(stream);
    if (drinks != null)
    {
        this.ShowPopup(new PopupPageAlert(drinks.First(x => x.id == randomdrink)));
    }
}

弹出代码:

public partial class PopupPageAlert : Popup
{
public drinksclass drink { get; set; }
public PopupPageAlert(drinksclass drink)
{
    InitializeComponent();
    this.drink = drink;
    this.BindingContext = this.drink;
}

private void CloseBtn_Clicked(object sender, EventArgs e)
{
    this.Close();
}
bogh5gae

bogh5gae1#

您可以通过构造函数传递参数,就像传递任何C#类一样

this.ShowPopup(new PopupPage(drinksclass));

然后在弹出页面的构造函数中

public drinksclass drink { get; set; }

public PopupPage(drinksclass drink)
{
   InitializeComponent();

   this.BindingContext = this.drink = drink;

   ...
}

然后在XAML中

<Label Text="{Binding name}" .. />

相关问题