繁体   English   中英

在Xamarin.Forms中使用加载的触发器

[英]Using Loaded Trigger in Xamarin.Forms

在WPF或UWP中使用MVVM Light时,可以使用触发事件“加载”,例如使用导航到页面的触发器。

像这样:

<i:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="Loaded">
                <core:InvokeCommandAction Command="{Binding LoadedCommand}" />
            </core:EventTriggerBehavior>
</i:Interaction.Behaviors>

这是名称空间:

xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

但是如何在Xamarin.Forms执行此Xamarin.Forms 一样吗 我找不到一个例子。

UPDATE

我的xaml页面:

对EventToCommandBehavior的引用:

xmlns:behaviors="clr-namespace:XamarinTest.Behaviors"

<ContentPage.Behaviors>
        <behaviors:EventToCommandBehavior EventName="OnAppearing"
                                          Command="{Binding LoadedCommand}" />
</ContentPage.Behaviors>
<ContentPage.Content>
    <StackLayout>
        <Label Text="Welcome to Xamarin.Forms!"
               VerticalOptions="CenterAndExpand" 
               HorizontalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

这是我的ViewModel:

public RelayCommand LoadedCommand { get; }

public UserMainViewModel()
{
    LoadedCommand = new RelayCommand(LoadData);
}

private async void LoadData()
{
    //...
}

xaml Page和ViewModel之间的绑定在Page1.xaml.cs中是这样的:

public Page1()
{
    InitializeComponent();
    BindingContext = App.Locator.UserMainViewModel;
}

错误:

 at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00017] in <657aa8fea4454dc898a9e5f379c58734>:0 
  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0007a] in <657aa8fea4454dc898a9e5f379c58734>:0 
  at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <657aa8fea4454dc898a9e5f379c58734>:0 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <657aa8fea4454dc898a9e5f379c58734>:0 
  at MyProject.Services.NavigationService.NavigateTo (System.String pageKey, System.Object parameter) [0x000e8] in C:\Projects\MyProject\MyProject\MyProject\Services\NavigationService.cs:89 
  at MyProject.Services.NavigationService.NavigateTo (System.String pageKey) [0x00001] in C:\Projects\MyProject\MyProject\MyProject\Services\NavigationService.cs:42 
  at MyProject.ViewModels.LoginViewModel.Login () [0x00002] in C:\Projects\MyProject\MyProject\MyProject\ViewModels\LoginViewModel.cs:25 

您必须按照此处所述使用EventToCommandBehavior https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/behaviors/reusable/event-to-command-behavior/

并在页面中实现以下xaml:OnAppearing仅存在于页面上。 (未加载事件)

<ContentPage.Behaviors>
   <local:EventToCommandBehavior EventName="Appearing" Command="{Binding LoadedCommand}" />
<ContentPage.Behaviors>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM