- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的移动应用程序中创建自定义导航/“后退”按钮。我想将该命令绑定(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();
}
}
ICommand
在 BindingContext
中解析,导致调用 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
中编码的弹出窗口的成功调用。
关于c# - Xamarin:ImageButton 不从样式 setter 调用命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72478496/
因此,我需要获取在为其赋值时调用的 setter 的名称。像这样: var b = {}; var a = { set hey(value) { b[] = value; } } 我希望 se
我是 Android 编程的新手(~ 2 个月)有必要为几十个不同的变量设置 getter 吗? 例如—— //Yes I realise that this isn't 'dozens' publi
import Control.Lens import Control.Lens.TH data Foo = Foo { _bar, _baz :: Int } makeLenses ''
我有点困惑:我可以覆盖 setter/getter 但仍然使用 super setter/getter 吗?如果是 - 怎么办? 用例: class A { void set value(num
我有一个接收消息的应用程序。消息中存在可编辑的字段。当字段更改时,应将其保存到数据库中。不幸的是,setter 仅在 setter 的范围内更改给定字段的值。知道为什么会发生这种情况吗?这是 gett
C# 中有没有一种方法可以让 setter 从“某物”继承,这样每次为特定基类及其继承者调用 setter 时,我都可以运行一些代码? 我想做的是在我的基类上有一个名为 IsValid 的 bool
可能是一个我无法解决的非常简单的问题 - 我从 C# 开始,需要使用 getter/setter 方法向数组添加值,例如: public partial class Form1 : Form {
这两个属性实现有什么区别? public override string A { get { return "s"; } set { } } public override strin
是否可以使用 abc.abstractproperty 创建一个具体的 getter 但将 setter 抽象为每个继承类的不同。我为每个子类处理不同的 val 设置。 例如。 @abstractpr
我在某处看到类似下面的内容,想知道它是什么意思。我知道他们是getter和setter,但是想知道为什么字符串Type是这样定义的。谢谢你帮助我。 public string Type { get;
Public class Example { private int number; public Example(int number){ this.number =
假设我有这样的代码: public response MyMethod(Request req) { String id = req.getFirst().geId(); } 我已经模拟了主对
允许这样做: public int Age { get; set; } 但是应用程序是否为变量创建/分配空间?我经常这样做 private int age = 0; public int Age {
我有一个条件,我构造字符串 (finalValue) 的方式是基于我在输入中获得的非空值的数量。所以我想知道是否可以用一个不同的参数为字符串 (finalValue) 重载 setter 方法并根据我
例如,在这段代码中 var o = { set a(value) {this.b = value}, get a() {return this.b} } 是否有可能获得对 o.a 的 sett
我一直在努力了解 getter 和 setter,但没有深入了解。我读过 JavaScript Getters and Setters和 Defining Getters and Setters只是没
我想在我的类中添加一个 getter 和 setter。然而,setter 应该接收一个 querySelector,但 getter 返回一个新类型 pageSections。 我的问题是 gett
使用有什么好处: private var _someProp:String; public function set someProp(value:String):void { _somePr
当从域类调用它时,我想在我的 setter 中执行一些操作,而不是从 hibernate 中调用它时。此外,我正在使用 session 工厂,因此我无法使用 @PostLoad 来触发标志! 有人对此
人员类别: public class Person { private String firstName; private String lastName; public Pe
我是一名优秀的程序员,十分优秀!