简体   繁体   中英

CallMethodAction doesn't execute code-behind methods in Windows Phone 8.1

I created a simple XAML page:

    <Page.DataContext>
        <local:MainPageViewModel />
    </Page.DataContext>

    <Interactivity:Interaction.Behaviors>
        <Core:DataTriggerBehavior Binding="{Binding MyNumber}" Value="3">
            <Core:CallMethodAction MethodName="TestMethod" TargetObject="{Binding ElementName=page}" />
            <Core:CallMethodAction MethodName="ViewModelMethod" TargetObject="{Binding Mode=OneWay}" />
        </Core:DataTriggerBehavior>
    </Interactivity:Interaction.Behaviors>

    <Grid>

        <TextBox
            Margin="0"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Text="{Binding MyNumber,
                                Mode=TwoWay}"
            TextWrapping="Wrap" />

    </Grid>

And a ViewModel for this page:

    public class MainPageViewModel : INotifyPropertyChanged
    {
        private int _myNumber;

        public int MyNumber
        {
            get { return _myNumber; }
            set
            {
                _myNumber = value;
                RaisePropertyChanged("MyNumber");
                Debug.WriteLine("Property MyNumber changed.");
            }
        }

        public void ViewModelMethod()
        {
            Debug.WriteLine("ViewModelMethod called.");
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion INotifyPropertyChanged
    }

In the page code behind, I added the following method:

        public void TestMethod()
        {
            Debug.WriteLine("Method TestMethod called.");
        }

When I enter number 3 in the TextBox, only the second CallMethodAction gets executed.

The TextMethod in the code behind doesn't execute. Why? I never had this problem in Windows Phone 8.

Found a solution! In Windows Phone 8.0, you have to set the TargetObject property to call methods in the code behind. In Windows Phone 8.1, you don't set the TargetObject at all. The code that works is:

<Core:CallMethodAction MethodName="TestMethod" />

Though, I still think it should at least throw an error, if a method doesn't exist...

This works for me, Windows Phone 8.1...

XAML page

<Interactivity:Interaction.Behaviors>
    <Core:EventTriggerBehavior EventName="Tapped">
        <Core:CallMethodAction MethodName="TestMethod" TargetObject="{Binding ElementName=page}"/>
    </Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>

Is base on Tappe event, but I think in will works on your case.

For the method, important! make it public.

public void TestMethod(object sender, TappedRoutedEventArgs e)
{
    Debug.WriteLine("TestMethod");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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