﻿// We rely on code behind to show the flyout

// Listen for the ContextRequested event so we can change the launch behavior based on whether it was a 
// left or right click.
this.FindControl<Avalonia.Controls.Button>("myImageButton").ContextRequested += OnMyImageButtonContextRequested;

private void MyImageButton_Click(object sender, RoutedEventArgs args)
{
    ShowMenu(true);
}

private void OnMyImageButtonContextRequested(object sender, ContextRequestedEventArgs e)
{
    ShowMenu(false);
    e.Handled = true;
}

private void ShowMenu(bool isTransient)
{
    var flyout = Resources["CommandBarFlyout1"] as CommandBarFlyout;
    flyout.ShowMode = isTransient ? FlyoutShowMode.Transient : FlyoutShowMode.Standard;

    flyout.ShowAt(this.FindControl<Image>("Image1"));
}
