我正在try 在我的移动应用程序中创建自定义导航/"后退"按钮.我想将该命令绑定为自定义样式中的setter属性,然后将该样式应用于称为HeaderNavigation的更大自定义元素中的ImageButton元素.

一切都是这样构建的,我可以看到我的ImageButton及其应用的样式.当我查看ImageButton的样式but时,我还可以看到应用于ImageButton的命令,如果单击ImageButton,则不会调用该命令.

我已经try 了一些绑定,因为我假设这就是问题所在,但还没有骰子,所以我担心我正在try 的是不可能的,或者可能我遗漏了一些愚蠢的东西.

即使有人能给我一些关于移动绑定的调试技巧,那也会很有帮助.

谢谢

代码如下:

app.xaml

<Style x:Key="NavButton" TargetType="ImageButton" BasedOn="{StaticResource IconButton}">
        <Setter Property="Source" Value="back.png"/>
        <Setter Property="HorizontalOptions" Value="EndAndExpand"/>
        <Setter Property="Command" Value="{Binding Source={RelativeSource AncestorType={x:Type local:App}},Path=BindingContext.OnBackButtonPressed}"/>
        <Setter Property="BackgroundColor" Value="{StaticResource Background}"/>
</Style>

App.xaml.cs

public async void OnBackButtonPressed(object sender, EventArgs e)
{
        // I do not get called :(
        _ = await Navigation.PopAsync();
}

HeaderNavigation.cs

public class HeaderNavigation : StackLayout
{
    private ImageButton _returnButton;

        public HeaderNavigation()
        {
            _returnButton = new ImageButton();

            _returnButton.Style = (Style)Application.Current.Resources["NavButton"];

            Children.Add(_returnButton);
        }
    }
}

推荐答案

我的理解是,您的目标是让自定义NavigationHeader中的back按钮调用App类中的OnBackButtonPressed方法:

public partial class App : Application
{
    public async void OnBackButtonPressed(object sender, EventArgs e)
    {
        // This pop up will be the test.
        await MainPage.DisplayAlert("Message from button", "It worked", "OK");
    }
}

我已经测试了Download from GitHub种方法来实现您想要的结果,其中考虑了您的自定义NavigationHeader在某种视图中被消费的可能性(例如ContentPage).

try 在样式声明中进行以下更改:

<Setter Property="Command" Value="{Binding BackButtonPressedCommand}" />

这样,当您使用自定义NavigationHeader时,例如在您的MainPage.xaml...

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:custom_back_button="clr-namespace:custom_back_button"
         x:Class="custom_back_button.MainPage">

<StackLayout>
    <custom_back_button:HeaderNavigation  VerticalOptions="CenterAndExpand"/>
</StackLayout>

...绑定命令将在主页的上下文中调用.(同样,如果在大约other页上使用它,它将吸收that页的BindingContext.)

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        BindingContext = new MainPageBinding();
        InitializeComponent();
    }
}

BindingContext中解析ICommand,从而调用this类中的OnBackButtonPressed.现在使用((App)Application.Current)将操作传递给App类,如图所示.

class MainPageBinding
{
    public MainPageBinding()
    {
        BackButtonPressedCommand = new Command(OnBackButtonPressed);
    }

    public ICommand BackButtonPressedCommand { get; private set; }
    private void OnBackButtonPressed(object o)
    {
        ((App)Application.Current).OnBackButtonPressed(o, EventArgs.Empty);
    }
}

按下后退按钮显示成功调用编码为App.OnBackButtonPressed的弹出窗口.

enter image description here

Csharp相关问答推荐

在C# 11之前, struct 中的每个字段都必须显式分配?不能繁殖

O(N)测试失败

C#使用属性和值将JSON转换为XML

在. net毛伊岛窗口的深度链接已经创建""

ASP.NET Core 8.0 JWT验证问题:尽管令牌有效,但SecurityTokenNoExpirationError异常

Polly v8—使用PredicateBuilder重试特定的状态代码

Blazor服务器端的身份验证角色

有没有办法在WPF文本框中添加复制事件的处理程序?

.NET 6控制台应用程序,RabbitMQ消费不工作时,它的程序文件中的S

C#EF Core WHERE IN LINQ FROM LIST WITH.CONTAINS不返回任何内容

如何在C#中转换泛型包装类内部的派生类

有没有类似于扩展元素的合并元组的语法?

是否有必要在ASP.NET Core中注册可传递依赖项?

为什么方法的值在SELECT方法中不会更改?

EF Core:如何对关系属性进行建模?

根据优先级整理合同列表

NETSDK1201:对于面向.NET 8.0和更高版本的项目,默认情况下,指定RUNTIME标识符将不再生成自包含的应用程序

将列表转换为带有逗号分隔字符串形式的值的字典

如何根据分割文本的块数来计算文本的大小?

能否将我图表中的星号与X轴上一天中的第二位数字对齐?