简体   繁体   English

CallMethodAction不执行Windows Phone 8.1中的代码隐藏方法

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

I created a simple XAML page: 我创建了一个简单的XAML页面:

    <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: 还有此页面的ViewModel:

    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. 当我在文本框中输入数字3时,仅执行第二个CallMethodAction。

The TextMethod in the code behind doesn't execute. 后面代码中的TextMethod不执行。 Why? 为什么? I never had this problem in Windows Phone 8. 我在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. 在Windows Phone 8.0中,您必须将TargetObject属性设置为在后面的代码中调用方法。 In Windows Phone 8.1, you don't set the TargetObject at all. 在Windows Phone 8.1中,根本不需要设置TargetObject。 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... 这对我有用,Windows Phone 8.1 ...

XAML page XAML页面

<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. 基于Tappe事件,但我认为in可以解决您的情况。

For the method, important! 对于方法,很重要! make it public. 公开。

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

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

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