TMa*_*dox 2 c# xamarin.forms floating-action-button
我正在使用 NuGet 包https://github.com/jamesmontemagno/FloatingActionButton-for-Xamarin.Android来实现浮动操作按钮。我已经添加了它,但我无法让 clicked 事件工作。
我的页面.xaml:
<fab:FloatingActionButtonView ImageName="settings_24.png"
ColorNormal="{StaticResource PrimaryColor}"
ColorPressed="{StaticResource PrimaryDarkColor}"
ColorRipple="{StaticResource PrimaryDarkColor}"
x:Name="FABaddTx"
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize"
Clicked="FAB_Clicked"/>
Run Code Online (Sandbox Code Playgroud)
MyPage.xaml.cs:
private void FAB_Clicked(object sender, EventArgs e)
{
var page = new AddTransactionPage();
Navigation.PushAsync(page);
}
Run Code Online (Sandbox Code Playgroud)
我发现这FloatingActionButtonView.Clicked是一个属性而不是一个事件,但它是如何工作的呢?没有我可以使用的aItemTapped或ItemSelectedEvent 。
我已经浏览了 GitHub 存储库上提供的示例代码,遗憾的是只有 Xamarin.Android 代码,而我需要 Xamarin.Forms。
根据源存储库中的这个 Xamarin.Forms 示例,它是一个操作委托属性。
/// <summary>
/// Action to call when clicked
/// </summary>
public Action<object, EventArgs> Clicked { get; set; }
Run Code Online (Sandbox Code Playgroud)
您可以在 Page 的构造函数中设置委托。
但是,鉴于它不是一个实际的事件处理程序而只是一个 void 方法,async void如果您想安全地执行异步操作,则不建议将其设为方法。
相反,创建一个事件和异步事件处理程序,以便可以等待异步操作
MyPage.xaml.cs:
public MyPage() {
//Subscribing to our event here
FabClicked += OnFabClicked;
//FABaddT is already defined in the XAML so just setting the delegate here
FABaddT.Clicked = (sender, args) => {
//Raising the event here and passing sender and event arguments on
FabClicked(sender, args);
};
}
private event EventHandler FabClicked = delegate { };
private async void OnFabClicked(object sender, EventArgs args) {
var page = new AddTransactionPage();
await Navigation.PushAsync(page);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
915 次 |
| 最近记录: |