gpt4 book ai didi

c# - Xamarin:ImageButton 不从样式 setter 调用命令

转载 作者:行者123 更新时间:2023-12-05 04:28:55 24 4
gpt4 key购买 nike

我正在尝试在我的移动应用程序中创建自定义导航/“后退”按钮。我想将该命令绑定(bind)为自定义样式中的 setter 属性,然后将该样式应用于名为 HeaderNavigation 的更大自定义元素中的 ImageButton 元素。

一切都建立起来,这样我就可以看到我的 ImageButton 应用了它的样式。当我查看其样式时,我还可以看到应用于 ImageButton 的命令但是,如果我单击 ImageButton,则不会调用该命令。

我试了一下绑定(bind),因为我假设这就是问题所在,但还没有骰子,所以我担心我正在尝试的东西是不可能的,或者我可能缺少一些愚蠢的东西。

即使有人可以给我一些移动绑定(bind)的调试技巧,那也会很有帮助。

谢谢!

代码如下:

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 中的后退按钮调用 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 可能会在某种 View (例如 ContentPage)中被使用。

尝试在您的 Style 声明中进行此更改:

<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>

...绑定(bind)的命令将在 MainPage 的上下文中调用。 (同样,如果您在某个其他 页面上使用它,它将吸收那个 页面的 BindingContext。)

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

ICommandBindingContext 中解析,导致调用 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

关于c# - Xamarin:ImageButton 不从样式 setter 调用命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72478496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com